• 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 in oracle tutorials ?

 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please refer oracle tutorials given at http://docs.oracle.com/javase/tutorial/essential/regex/test_harness.html. when i ran the program and entered the given regex and input string i get the given below result :
Enter your regex: a??
Enter input string to search: aba
I found the text "" starting at index 0 and ending at index 0.
I found the text "" starting at index 1 and ending at index 1.
I found the text "" starting at index 2 and ending at index 2.
I found the text "" starting at index 3 and ending at index 3.

please explain how i'm getting zero-length matches ? i know i'm using reluctant quantifier.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote:please explain how i'm getting zero-length matches ? i know i'm using reluctant quantifier.


You're also matching on 'a' once or not at all.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:

gurpeet singh wrote:please explain how i'm getting zero-length matches ? i know i'm using reluctant quantifier.


You're also matching on 'a' once or not at all.




but as you can see from the output, all i'm getting is zero length matches. why it is not matching a. i'm not able to understand.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried to print the match using start and end ?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Helen Ma wrote:Have you tried to print the match using start and end ?




yes helen. the link i gave contains the source code java file for the code given on the oracle tutorials. they have used start and end methods to print the location of the match.
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, I guess the match is



Please verify this.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Helen Ma wrote:So, I guess the match is



Please verify this.




but when i run the program named as TestHarness given on oracle site , it does not produce the above output. as mentioned in my original post it produces following output. you can verifiy by running it.


Enter your regex: a??
Enter input string to search: aba
I found the text "" starting at index 0 and ending at index 0.
I found the text "" starting at index 1 and ending at index 1.
I found the text "" starting at index 2 and ending at index 2.
I found the text "" starting at index 3 and ending at index 3

 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I refer to this web site:
http://www.regular-expressions.info/reference.html

I guess a?? is evaluated as "a" as optional. So, regex a?? means evaluate a substring without or with a. So, "a", "b" , "a" is evaluated in this way:
1. "a" is without "a" --pass evaluation
2. "b" has no "a". --pass evaluation

I guess it has something to do with the compiler design.

 
The longest recorded flight time of a chicken is 13 seconds. But that was done without this 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