aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regex expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regex expressions " Watch "Regex expressions " New topic
Author

Regex expressions

jayalakshmi charugundla
Ranch Hand

Joined: Jul 22, 2009
Posts: 57
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

Joined: Feb 23, 2009
Posts: 1183
Could you post what exactly it states ? I don't have the book at hand.


JDBCSupport - An easy to use, light-weight JDBC framework -
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

This should clear your doubts, I guess (I don't have a book):
http://java.sun.com/docs/books/tutorial/essential/regex/quant.html


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

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


KARTHICK.C , SCJP6-93%
(Born to Win)
Steven Lennon
Greenhorn

Joined: Aug 23, 2009
Posts: 5
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

Joined: May 31, 2007
Posts: 3216
Read "Differences Among Greedy, Reluctant, and Possessive Quantifiers" section in http://java.sun.com/docs/books/tutorial/essential/regex/quant.html



Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Steven Lennon
Greenhorn

Joined: Aug 23, 2009
Posts: 5
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

Joined: Apr 20, 2009
Posts: 804
    
    1

Anybody can explain this?
Steven Lennon
Greenhorn

Joined: Aug 23, 2009
Posts: 5
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

Joined: Aug 03, 2008
Posts: 9280
    
  17

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 )


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
 
subject: Regex expressions
 
Similar Threads
Shallow Cloning
White spaces in XSLT in text mode
Multithreading
Query regarding DataInputStream class on SCJP 5
WA #1.....word association