Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

Difference between regex quantifiers plus and star

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The * quantifier matches zero or more occurences.

In practice, this means that

d*

will match every possible input, including the empty string. So your regex matches at the start of the input string and returns the empty string.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...