This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex expressions

 
jayalakshmi charugundla
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • 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.




 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post what exactly it states ? I don't have the book at hand.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • 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....
 
Steven Lennon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.
 
arulk pillai
Author
Ranch Hand
Posts: 3388
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Anybody can explain this?
 
Steven Lennon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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 )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic