Reputation: 5428
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?
Upvotes: 0
Views: 216
Reputation: 2968
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>[^\"]*)"
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().
Upvotes: 5
Reputation: 13858
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?
Upvotes: 0
Reputation: 7376
By convention, AFAIK in regex engines the 0th group is always the whole matched string. Nested groups start at 1.
Upvotes: 0
Reputation: 8141
m.group(1) will give you the Match. If there are more than one matchset (), it will be m.group(2), m.group(3),...
Upvotes: 1