• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex return to pattern - Solved

 
Rodrigo Tassini
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"*" 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
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Rodrigo Tassini
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry guys, I'd make a mistake.

The correct pattern is
 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic