• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
J Westland
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% )
 
Henry Wong
author
Sheriff
Posts: 23289
125
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: 23289
125
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 ^_^
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!