Regular expression question

J

Jim Burgess

Why does:
puts /\w+? \w+?/.match "Ein kleiner Satz"
return "Ein k" and not "n k"
My understanding was that \w represents any alphanumerical character, +
means that it occurs at least once and the ? following the plus makes it
non-greedy.
I would have thought this returned "n k" although that is obviously not
correct.
Thanks in advance.
 
D

David A. Black

Hi --

Why does:
puts /\w+? \w+?/.match "Ein kleiner Satz"
return "Ein k" and not "n k"
My understanding was that \w represents any alphanumerical character, +
means that it occurs at least once and the ? following the plus makes it
non-greedy.
I would have thought this returned "n k" although that is obviously not
correct.

Eine kleine Misverstaendnis :)

\w+ means "one or more from the set (alphanumeric + underscore)".
The pattern will start trying to match at the beginning of the string,
where it finds \w+, then the space, then more \w+. It grabs all of Ein
because it hasn't found a space yet (but it also hasn't found any
reason to stop proceeding along the string, since "E", "Ei", and "Ein"
are all \w+). It only grabs the k in kleiner because that satisfies
the pattern in non-greedy fashion.

In this particular case, the first + doesn't matter, because the
pattern will keep going until it finds a space, so normal rules of
(non-)greediness don't apply.


David
 
J

Jim Burgess

Vielen Dank für deine Antwort :)
I was unaware that the reg ex would start from the point where it
encounters the first character satisfying \w.
Regular expressions - sometimes I hate them more than I love them.
Thanks very much for your help.
 
J

Jim Burgess

Thanks for the comprehensive explanation. That helps me greatly.
I am currently reading a ruby book (in German, just to make things a bit
simpler) and am on the reg ex chapter. It's hardcore!
Thanks again.
 
D

David A. Black

HI --

Es gibt ja auch noch de.comp.lang.ruby. :)

Btw, David you wrote


I believe you meant "the first ? doesn't matter" - as leaving out the "+"
would indeed make a difference but it's the non greediness addition which
does not make a difference to matching.

Yes -- I did indeed mean ?, not +. Thanks.


David
 
D

David A. Black

Hi --

Thanks for the comprehensive explanation. That helps me greatly.
I am currently reading a ruby book (in German, just to make things a bit
simpler) and am on the reg ex chapter. It's hardcore!

Oh, your English is plenty good to read Ruby books -- like
http://www.manning.com/black2, just to choose a random example :)


David
 
J

Jim Burgess

Oh, your English is plenty good to read Ruby books -- like
http://www.manning.com/black2, just to choose a random example :)

I must admit, I'm English, just happen to be living in Germany - hence
the good standard of English :)
But seriously, I have bookmarked the page you sent me and I will check
it out as I am always on the lookout for good books about Ruby.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,167
Messages
2,570,913
Members
47,454
Latest member
eibaloja

Latest Threads

Top