I am trying to use regular expressions in Java to match all string of the form <b><number></b>
that are contained within a <a><\a>
pair.
e.g. <a> kljsdlk <b>123</b> df <b>345</b> sdfklj</a>
should match twice with <b>123</b>
and <b>345</b>
, while <v> kljsdlk <b>123</b> df <b>345</b> sdfklj</v>
should yield no results (because there is no wrapping <a></a>
).
The following code is my current best result:
Pattern MY_PATTERN = Pattern.compile("(<a>.*(<b>[0-9]*<\\\\b>)?.*<\\\\a>)");
Matcher m = MY_PATTERN.matcher("<a> skdjlkasjflkj <b>200<\\b> sldfhjhfj d lkj b <b>300<\\b> fhih 9 09 <\\a>");
while (m.find()) {
for (int i=0; i< m.groupCount() ;i++){
String s = m.group(i);
System.out.println(s);
}
}
This code result with:
<a> skdjlkasjflkj <b>200<\b> sldfhjhfj d lkj b <b>300<\b> fhih 9 09 <\a>
<a> skdjlkasjflkj <b>200<\b> sldfhjhfj d lkj b <b>300<\b> fhih 9 09 <\a>
I would like it to result in:
<b>200<\b>
<b>300<\b>