EDIT: The RE in the original code is irrelevant (or that it makes any sense). Let's say you're matching (X)|(Y): two patterns that are combined in the RE with an OR. How to know which pattern was actually matched??
I'm trying to extract just the text that's mattching the RE within the brackets.
The problem i'm facing is that i cannot figure out which actual group was matched, since the group index is not constant because of the OR.
Ie in the line marked XXX m.group() returns the entire pattern
pat1
abcdef2
And m.group(1) produces
pat
null
And m.group(2) produces
null
de
And m.groupCount() is just a total number of groups in the RE, so it is useless for indexing.
What I want the loop to print is
pat
de
It's a shame java doesnt have the perl's m/ operator that'll extract the stuff and put it nicely into an array ;-)
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) throws IOException {
Pattern p = Pattern.compile("([pat]+)1|abc([de]+)f2");
String original = " pat1 abcdef2555";
Matcher m = p.matcher(original);
boolean result = m.find();
while (result) {
System.out.println(m.group()); // XXX want to print only matched GROUP!!
result = m.find();
}
}
}
jjjpatssjpttattpppt1andksjdhfabcdddddddddf2what would be your desired output? As your expression is currently, the matches would be successful and the text would bepttattppptanddddddddddrespectfully. I ask because like others here, I think you may have a poor understanding of what your expression is specifying and possibly regex syntax in general