jeudi 20 juillet 2017

Matching regular expressions in Android

I want to extract specific information from a string like this:

String content = "ObjectValue(Name1) ObjectValue(Name2) ObjectValue(Name3)";

I want to use regular expressions in order to extract only:

Name1 Name2 Name3

But I can't succeed. Here's my code:

private static final String OBJECT_VALUE = "ObjectValue";
String content = "ObjectValue(Name1) ObjectValue(Name2) ObjectValue(Name3)";
String patternString = OBJECT_VALUE+"\\((.+)\\)";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(content);

if(matcher.find()) {
        for (int i = 0; i < matcher.groupCount(); i++) {
            Log.d("Found items", matcher.group(i));
        }

    }

Here's the output:

Found items: ObjectValue(Name1) ObjectValue(Name2) ObjectValue(Name3)

So, first of all, I want to extract only the names, and I clearly missing something in the regex. Secondly, I want distinct elements of the group, while in the output, the group consist of a single element, is it possible?

Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire