Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Regex clarification  RSS feed

 
Ranch Hand
Posts: 49
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
 
What's that smell? Hey, sniff this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!