Regex "greedy" check
I'm trying to use a simple Regex to match a patttern but get some
unexpected results...
The search pattern and results are given below,
public class Test {
public static void main(String[] args) throws IOException {
Pattern p = Pattern.compile(".*xx");
Matcher m = p.matcher("yyxxxyxx");
while (m.find()){
System.out.println("match start");
System.out.println("Start = " + m.start());
System.out.println("End = " + m.end());
System.out.println("Group = " + m.group());
}
}
}
Result:
match start Start = 0 End = 8 Group = yyxxxyxx
Expected Result:
match start Start = 0 End = 4 Group = yyxx match start Start = 4 End = 8
Group = xyxx
Can someone explain how the regex operates ?
No comments:
Post a Comment