Error getting names with apostrophes when using regex java android

3

I have a problem with this regex code to get text strings with apostrophes obtained through streaming. The code I use is this:

public static Map<String, String> parseMetadata(String metaString) {
    Map<String, String> metadata = new HashMap<String, String>();
    String[] metaParts = metaString.split(";");
    Pattern p = Pattern.compile("^([a-zA-Z]+)=\'([^\']*)\'$");
    Matcher m;

    for (int i = 0; i < metaParts.length; i++) {
        m = p.matcher(metaParts[i]);
        if (m.find()) {
            metadata.put(m.group(1), m.group(2));
             Log.d("MAP", String.valueOf(metadata));
        }
    }

    return metadata;
}

This part of the code is what gives me problems

Pattern p = Pattern.compile("^([a-zA-Z]+)=\'([^\']*)\'$");

And this is an example of text (there can only be a single title in the string):

StreamTitle='The Swirling Eddies - Don't Ask Me How I Feel';

The problem is that if there is a song that has the title with apostrophe , it does not show it to me and the titles with hyphens, brackets or accents if.

For example if the title is like this: Can't play , it does not show me the text string, and what I'm looking for is to show it with everything and apostrophe.

    
asked by Quimbo 20.05.2017 в 03:23
source

1 answer

2

In regex it is not necessary to escape ' with \ . So, if the format you're looking for is something like:

title='Can\'t be'

the Pattern would be

Pattern p = Pattern.compile("^([a-zA-Z]+)='(.*)'$");

That gives you in group 1 the name of the tag and in group 2 the content between the ' .

    
answered by 20.05.2017 / 04:37
source