• 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 Doubt

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The above code does not matches any numbers, why so ?

"\\d*?" definitely means zero or more digits reluctantly, then why doesn't it matches 4 in the source string?

However if i change the pattern to "(\\d*)?" it matches, i can't understand why?

PS: i know this has been posted earlier, but in that post, no fruitful conclusion was drawn as to why is this happening?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reluctant quantifiers are different from greedy quantifiers -> http://www.devarticles.com/c/a/Java/Advanced-Regex/3/

Also, take a look at the Pattern class javadoc. It lists the reluctant / greedy quantifiers
 
Prateek Rawal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have another problem arising:



When i write java Now a* ababaaaab,
i get the following output
aarzoo1<songs.pk>.mp3yers

Let alone me matching, it is reading the pattern as one of the songs on my desktop(this code file is also on desktop), but when i write
java Now b* ababaaaab
i get the desired output along with desired matching........

Why is this weird thing happening?
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use double quotes when passing arguments from the command line and you will get desired output
java Now "a*" ababaaaab
instead of
java Now a* ababaaaab

java command treats * and ? characters in the same way as dir or ls commands in unix or dos.
Try to run this program from the command line with * argument and you will see that it will print list of files in your current working directory:

But if you invoke it with "*" argument, it will simply print *.
 
Prateek Rawal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!!!

But when i write java Now (b*) "abbabaaabbb", it works, why?

Is '(' valid instead of " ?

But it doesn't work with java <b*> "abbaaaabb", why so?
 
Beauty is in the eye of the tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic