• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Regex Question: Not sure if exam is this indepth but I am a little confused...  RSS feed

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Results are (both for expression"yyxxxyxx") :

m2 1 m2 3 m2 5 Pattern.compile(".xx*?")
m3 0 m3 4 for Pattern.compile(".*?xx")

So...where I put the *? quantifier makes a big difference here. I *think* the first expression "sees" yyxx then yyxxxyxx. But how does the second pattern read? Does it "see" yyxx yxxx yyxxxyxxx?

Could anyone point me to an explanation?

(not sure if SCJP goes this indepth but I don't like not understanding things a 100% )
 
author
Sheriff
Posts: 23588
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So...where I put the *? quantifier makes a big difference here....

Could anyone point me to an explanation?



Well, it makes a "big difference" because the two regexes are totally different.



The pattern here is ... match any character, followed by a letter "x", followed by zero or more matches of the letter "x" (and do it reluctantly).



The pattern here is... match zero or more of any character (and do it reluctantly), followed by "xx".

Henry
 
Henry Wong
author
Sheriff
Posts: 23588
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I *think* the first expression "sees" yyxx then yyxxxyxx.



The first "sees" "yx", then "xx", then "yx" (at index locations 1, 3, and 5 respectively).

But how does the second pattern read? Does it "see" yyxx yxxx yyxxxyxxx?



The second "sees" "yyxx", then "xyxx" (at index locations 0, and 4 respectively).

Henry
 
J Westland
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you that answer my question

Have worked a little with reg-ex using split() for reading in a text file...but not much ^_^
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!