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

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

I'm preparing for the SCJP 1.6 exam and have some doubt in the page no:500 which is in exam watch in the K&B book . I dont understand. Please explain this concept.




 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you post what exactly it states ? I don't have the book at hand.
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should clear your doubts, I guess (I don't have a book):
http://java.sun.com/docs/books/tutorial/essential/regex/quant.html
 
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jayalakshmi charugundla wrote:Hi All,

I'm preparing for the SCJP 1.6 exam and have some doubt in the page no:500 which is in exam watch in the K&B book . I dont understand. Please explain this concept.



please post the question too....... apart from quoting the source.....

java RegEx "a?" "aba"....

here "?" is a greedy quantifier..... it always searches for ZERO or ONE(here) matches......

remember the rule "ALL THE GREEDY QUANTIFIERS CHECKS ONE INDEX PAST THE STRING TO FIND A MATCH"

so in case of "?" and "*" they wil always find a zero length match at the end(that is one index past the given string)

if you try the same with "?" quantifier, i suppose you will get the same answer.....

thanks friend....
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I too am having some trouble with Regular Expressions. For instance I ran the following regular expressions. I understand the result for (ab)* but when I switch from a greedy quantifier to a reluctant quantifier it finds nothing at each index and I am not able to understand why. Can someone please explain this to me?



Many thanks.
 
Author
Posts: 3473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read "Differences Among Greedy, Reluctant, and Possessive Quantifiers" section in http://java.sun.com/docs/books/tutorial/essential/regex/quant.html


 
Steven Lennon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks, I have been referring to that tutorial but still don't understand why the second regular expression didn't find an 'ab' match. A reluctant qualifier consumes the string one character at a time but I still thought it would find an 'ab' match.

Any help will be appreciated. Thanks.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anybody can explain this?
 
Steven Lennon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for prompting on this question. I still haven't been able to find an answer to explain this reg exp output. Either I am missing something really fundamental here or it is not the correct output for such an expression.

Thanks and Regards
 
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
This is how I think it works (I've not read much about Regex so might be wrong)

Regex Engine's first priority is the overall success of the regex. Reluctant quantifiers try to match as less as possible. They'll only match more, if that can effect the overall success of the regular expression. Suppose your regex is a*?, and your input is aaaa, then it will try to match as less as possible so it'll result only in zero length matches. But if your regex is a*?b, and input is aaabaab, then you'll get aaab and aab as matches, this time a*? matched more because otherwise the whole regex would've failed. Since in the earlier case (a*? and aaaa), zero length matches still lead to the success of the regex, so regex engine didn't match anything. This zero length match thing will only happen in case of (ab)*? and not in (ab)+?, as + doesn't allow zero length matches, so regex engine will have to match 6 pairs of "ab"s (on input ababababababa)to make the regex successful. To test on my point about a*?b, you can try the regex (ab)*?b on the input abbabbabababa (note: this input is slightly different from the one in the original question)...

(PS: Henry solved my misconception about this here which made life a lot easier with regex questions for me )
 
That new kid is a freak. Show him this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic