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

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



Can anyone explain me the flow of this program??
What is happening in Line1 and Line2. I did read the API for the same.
It says,

Attempts to find the next subsequence of the input sequence that matches the pattern.

. But in this case which is that next subsequence??
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you consider ab15bss229 as the input sequence, then a part of this sequence eg 15 or 229 is the sub sequence which matches the pattern \\d*
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But this regex will also match an empty string, so the output of the program should be:

01234
4

Where the 0 in the first line is the starting index of the first match in the orginal string, and the 1234 is the actual matched sub-string.
The second line only displays the starting index of the second match, because the second match is an empty string.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you consider ab15bss229 as the input sequence, then a part of this sequence eg 15 or 229 is the sub sequence which matches the pattern \\d*



The answer is 0
1
215
4
5
6
7229
10

But now when I replace it with \\d*?, the answer is 012345678910?? what is happening in this case, why is not returning anything???
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*? is called reluctant pattern, means it tries to find as minimum as possible.

* is zero or more, so *? will go in minimum side, so for \\d*?, it will always try to find zero digits.
+ is one or more, so +? will find one digit always, as one is minimum here.
? is zero or one, so ?? will find zero occurrence always.


 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.Punit.
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i thing you would have understood punit's explaination.
just to add... check this faq
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh! Thanks James.
I was actually having a tough time understanding Regex concepts because I started it today. I was delaying studying this concept, as the name REGEX itself made me jittery.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhi vijay wrote:Oh! Thanks James.
I was delaying studying this concept, as the name REGEX itself made me jittery.




 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic