Regex oddity

Anybody know why this occurs?

‘a’.scan /.*/
=> [“a”, “”]

Is this normal/expected?

Thanks!
-roger-

On Fri, Jul 22, 2011 at 12:41 PM, Roger P.
[email protected]wrote:

Anybody know why this occurs?

‘a’.scan /.*/
=> [“a”, “”]

Is this normal/expected?

Not saying this is normal, but my suspicion is:

#scan first matches “a” (whole string), then tries to match on remainder
(“”) and matches (since ‘*’ matches 0 character) giving you another
match of
“”.

This behavior seems buggy IMHO. I’d think #scan would stop after the
whole
string was consumed. However, I do like that

“”.scan /.*/ => [“”]

On Fri, Jul 22, 2011 at 1:50 PM, Kendall G.
[email protected]wrote:

Not saying this is normal, but my suspicion is:

#scan first matches “a” (whole string), then tries to match on remainder
(“”) and matches (since ‘*’ matches 0 character) giving you another match
of
“”.

Seems correct:

“a”.scan(/.*/) # => [“a”, “”]
“a”.scan(/.+/) # => [“a”]

On Fri, Jul 22, 2011 at 8:41 PM, Roger P. [email protected]
wrote:

Anybody know why this occurs?

‘a’.scan /.*/
=> [“a”, “”]

Is this normal/expected?

Yes. Scanning will start at pos 0 of the string and match as long as
it can (i.e. until the end of the string): you get “a”. Next,
position is increase (now 1, at the end) and since .* matches the
empty string alas we have a match here as well: “”. Next position is
after the end => no more matches.

If you want to see regexp engine matching in action you can use regexp
coach which is a nice tool to explore such things (albeit not with
ruby’s regexp engine).

Kind regards

robert