Anybody know why this occurs?
'a'.scan /.*/
=> ["a", ""]
Is this normal/expected?
Thanks!
-roger-
···
--
Posted via http://www.ruby-forum.com/\.
Anybody know why this occurs?
'a'.scan /.*/
=> ["a", ""]
Is this normal/expected?
Thanks!
-roger-
--
Posted via http://www.ruby-forum.com/\.
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 12:41 PM, Roger Pack <rogerpack2005@gmail.com>wrote:
Anybody know why this occurs?
>> 'a'.scan /.*/
=> ["a", ""]Is this normal/expected?
--
Kendall Gifford
zettabyte@gmail.com
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
On Fri, Jul 22, 2011 at 8:41 PM, Roger Pack <rogerpack2005@gmail.com> wrote:
Anybody know why this occurs?
'a'.scan /.*/
=> ["a", ""]
Is this normal/expected?
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Seems correct:
"a".scan(/.*/) # => ["a", ""]
"a".scan(/.+/) # => ["a"]
On Fri, Jul 22, 2011 at 1:50 PM, Kendall Gifford <zettabyte@gmail.com>wrote:
On Fri, Jul 22, 2011 at 12:41 PM, Roger Pack <rogerpack2005@gmail.com > >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
"".