0

Java Code:

String imagesArrayResponse = xmlNode.getChildText("files");
Matcher m = Pattern.compile("path\":\"([^\"]*)").matcher(imagesArrayResponse);

while (m.find()) {
    String path = m.group(0);
}

String:

[{"path":"upload\/files\/56727570aaa08922_0.png","dir":"files","name":"56727570aaa08922_0","original_name":"56727570aaa08922_0.png"}{"path":"upload\/files\/56727570aaa08922_0.png","dir":"files","name":"56727570aaa08922_0","original_name":"56727570aaa08922_0.png"}{"path":"upload\/files\/56727570aaa08922_0.png","dir":"files","name":"56727570aaa08922_0","original_name":"56727570aaa08922_0.png"}{"path":"upload\/files\/56727570aaa08922_0.png","dir":"files","name":"56727570aaa08922_0","original_name":"56727570aaa08922_0.png"}]

m.group returns

path":"upload\/files\/56727570aaa08922_0.png"

instead of captured value of path. Where I am wrong?

1
  • 1
    path = m.group(1); Commented Dec 17, 2015 at 10:12

4 Answers 4

5

See the documentation of group( int index ) method

When called with 0, it returns the entire string. Group 1 is the first.

To avoid such a trap, you should use named group with syntax : "path\":\"(?<mynamegroup>[^\"]*)"

javadoc:

Capturing groups are indexed from left to right, starting at one. Group zero denotes the entire pattern, so the expression m.group(0) is equivalent to m.group().

Sign up to request clarification or add additional context in comments.

Comments

1

m.group(1) will give you the Match. If there are more than one matchset (), it will be m.group(2), m.group(3),...

Comments

0

By convention, AFAIK in regex engines the 0th group is always the whole matched string. Nested groups start at 1.

Comments

0

Check out the grouping options in Matcher.

 Matcher m = 
   Pattern.compile(
    //<-     (0)     ->  that's group(0)
    //          <-(1)->  that's group(1)
     "path\":\"([^\"]*)").matcher(imagesArrayResponse);

Change your code to

while (m.find()) {
  String path = m.group(1);
}

And you should be okay. This is also worth checking out: What is a non-capturing group? What does a question mark followed by a colon (?:) mean?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.