Difference Between Regular Expression Quantifiers Plus and Stars

I am trying to extract an error number from lines like "Wrong parameters - Error 1356":

 Pattern p = Pattern.compile("(\\d*)");
 Matcher m = p.matcher(myString);
 m.find();
 System.out.println(m.group(1));

And it doesn't print anything, which has become strange to me, since it *means * - Matches the preceding element zero or more timesfrom the Wiki

I also went to www.regexr.com and regex101.com and tested it, and the result was the same, nothing for this expression\d*

Then I start testing different things (all the tests done on the sites I mentioned):

  • (\d)* does not work
  • \d{0,} does not work
  • [\d]* does not work
  • [0-9]* does not work
  • \d{4} works
  • \d+ works
  • (\d+) works
  • [0-9]+ works

So, I'm starting to search the Internet if I find an explanation. The best I could find was here in the Quantifier section, which says:

\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.

Question

, ( (but none if necessary)). , , , ?

, , SO, : Regex: , .. \d **, .

+4
5

* .

,

\d*

, . .

+5

but none if necessary , , . , \d* , zero or more occurrences .

,

\d*[a-z]*

abcdef

\d+[a-z]*

abcdef

\d+ , .

+2
\d* Eat as many digits as possible (but none if necessary)

\d* , . (.. ). .

\d+

. , .

0

/d + , , .

/d * ( ), . .Net Regex .

0

:

\ d * implies zero or more times

\ d + means one or more times

0
source

Source: https://habr.com/ru/post/1546574/


All Articles