N
NeoGeoSNK
Hello,
I have learned Java Regular expression for a long time, but still
confused about Quantifiers:
import java.util.regex.*;
public class NRGRegex{
public static void main(String[] args){
Pattern p = Pattern.compile("a??");
String a = "aaa";
Matcher m = p.matcher(a);
while(m.find()){
System.out.println("found char = " + m.group() + " at " + m.start()
+ " and " + m.end()); }
}
}
the output result is:
found char = at 0 and 0
found char = at 1 and 1
found char = at 2 and 2
found char = at 3 and 3
here "a??" is Reluctant quantifiers but why all char 'a' not match
successful?
when I use greedy quantifiers Pattern p = Pattern.compile("a?");
the output result is:
found char = a at 0 and 1
found char = a at 1 and 2
found char = a at 2 and 3
found char = at 3 and 3
I think greedy quantifiers first eat whole string "aaa" at a time,
but why the emtry char at (0,0) (1,1) (2,2) can't match successful
compare with Reluctant quantifiers ?
Thanks!
I have learned Java Regular expression for a long time, but still
confused about Quantifiers:
import java.util.regex.*;
public class NRGRegex{
public static void main(String[] args){
Pattern p = Pattern.compile("a??");
String a = "aaa";
Matcher m = p.matcher(a);
while(m.find()){
System.out.println("found char = " + m.group() + " at " + m.start()
+ " and " + m.end()); }
}
}
the output result is:
found char = at 0 and 0
found char = at 1 and 1
found char = at 2 and 2
found char = at 3 and 3
here "a??" is Reluctant quantifiers but why all char 'a' not match
successful?
when I use greedy quantifiers Pattern p = Pattern.compile("a?");
the output result is:
found char = a at 0 and 1
found char = a at 1 and 2
found char = a at 2 and 3
found char = at 3 and 3
I think greedy quantifiers first eat whole string "aaa" at a time,
but why the emtry char at (0,0) (1,1) (2,2) can't match successful
compare with Reluctant quantifiers ?
Thanks!