I try to extract the error number from strings like "Wrong parameters - Error 1356"
:
Pattern p = Pattern.compile("(\d*)");
Matcher m = p.matcher(myString);
m.find();
System.out.println(m.group(1));
And this does not print anything, that became strange for me as the *
means * - Matches the preceding element zero or more times
from Wiki
I also went to the www.regexr.com and regex101.com and test it and the result was the same, nothing for this expression d*
Then I start to test some different things (all tests made on the sites I mentioned):
(d)*
doesn't work
d{0,}
doesn't work
[d]*
doesn't work
[0-9]*
doesn't work
d{4}
works
d+
works
(d+)
works
[0-9]+
works
So, I start to search on the web if I could find an explanation for this. The best I could find was here on the Quantifier section, which states:
d? Optional digit (one or none).
d* Eat as many digits as possible (but none if necessary)
d+ Eat as many digits as possible, but at least one.
d*? Eat as few digits as necessary (possibly none) to return a match.
d+? Eat as few digits as necessary (but at least one) to return a match.
The question
As english is not my primary language I'm having trouble to understand the difference (mainly the (but none if necessary)
part). So could you Regex expert guys explain this in simple words please?
The closest thing that I find to this question here on SO was this one: Regex: possessive quantifier for the star repetition operator, i.e. d** but here it is not explained the difference.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…