• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regex clarification

 
Ranch Hand
Posts: 49
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have tried the below code


But it is not matching any numbers, can onyone explain what the pattern "\\d*?" will match
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try (\\d*)? which says >=0 numbers followed by a zero or one character.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

narain ashwin wrote:

But it is not matching any numbers, can onyone explain what the pattern "\\d*?" will match



Change line 8 to:


and I hope that will give what you want.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

narain ashwin wrote:
But it is not matching any numbers, can onyone explain what the pattern "\\d*?" will match



Patterns \\d* and \\d*? both matches 0 or more digits.
But \\d* is 'greedy' and \\d*? is 'lazy'.
'Greedy' means that regex engine first consumes as many charachters from the input line as possible and tries to match the whole line to the pattern,
if it doesn't match then the engine 'backtracks' - cuts one char from the end and tries again ... and recursively again and again
until it find some match.
'Lazy pattern' consumes as less characters as possible - 0 at start - and tries to match.

Consider input line = '123a' and patterns \\d* and \\d*?
\\d* at first consumes the whole line - '123a' - it doesn't match.
Then cuts last character off - the next attempt is with line '123'. This matches with the pattern and result is 123.
\\d*? at first consumes as less as possible - the empty string '' before '1'. This matches with the pattern and result is ''
(\\d*? means 0 or more, and 0 is simply an empty string).



Lazy operator could be useful for example if you want to match the first tag <> from html string,
look at this:

<.*> greedy pattern doesn't work because we want only first <> tag
<.*?> lazy pattern match exactly what we want
 
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic