aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regex return to pattern - Solved 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 return to pattern - Solved" Watch "Regex return to pattern - Solved" New topic
Author

Regex return to pattern - Solved

Rodrigo Tassini
Greenhorn

Joined: Dec 29, 2008
Posts: 21
This is a example for SCJP for Java Platform, SE6 STUDY GUIDE by Richard F. Raposa and can be found at page 317.



In the books is write
The pattern matches any word that starts with any character between ‘ q ’ and ‘ v ’ (the [qrstuv] ) repeated any number of times (the “ * ” ), followed by any single character (the . ), and ending with the literal string “ ing ” . Using the tests array from the previous examples, the matches are
ring matches [qrstuv]*.ing
trying matches [qrstuv]*.ing


In my compilatin, the return was:
ing matches [qrstuv]*.*ing
ring matches [qrstuv]*.*ing
trying matches [qrstuv]*.*ing
running matches [qrstuv]*.*ing

Someone understand what's happened?

Thks in advance
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18709
    
  40

"*" does mean "repeated any number of times" including zero times. So, for the other two cases, where the word doesn't start with "q" to "v", the regex engine matches it zero times. This allows the "." to match the first character instead.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

4 values match the pattern give, so if you are getting 4 matches as output, then its correct, the answer might be wrong in the book...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Rodrigo Tassini
Greenhorn

Joined: Dec 29, 2008
Posts: 21
Sorry guys, I'd make a mistake.

The correct pattern is
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18709
    
  40



There is no need to change the subject to solved... there is a "resolve" button below, that you can use to declare the topic as solved.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regex return to pattern - Solved