I know that * is a greedy quantifier which matches as many as it can and must be zero or more, so there are empty strings after "wow". But can you tell me the reason I got "oo" in the output for both + and *?
I find greedy quantifiers hard to understand. I know + means 1 or more, and * mean zero or more, but I am not sure under what situation I am supposed to use each of them? Can you simplify the difference between them? (Any analogy or examples would helpful)
I used the code below to try those greedy quantifiers:
import java.util.regex.*;public class TestClass{ public static void main(String[] args) { Pattern p = Pattern.compile(args[0]); Matcher m = p.matcher(args[1]); boolean b = false; while(b = m.find()) { System.out.print(m.start()+" \""+m.group()+"\" "); } }}
soe [wow]* means that you are looking for the string which is made up of charcaters of "w" or "o" - any number of them,
therefore "oo" should match and "ww" should match or "wo" should match, it probably is not necessary to repeat "w" two times
inside the brackets
> But can you tell me the reason I got "oo" in the output for both + and *?
[wow]*
matches 0 or more 'w' or 'o''s.
The other w is actually redundant
Get rid of the square brackets if you want it to just match 'wow'
for_yan
As "[wow]*" does not need any single instance to match,
that's why it matches all these empty strings - frankly don't understand how many
empty strings it matches - I guess, one after each character with the exception of matches
matcgh - makes sense