"Martin Elzen" <martinelzen / hotmail.com> schrieb im Newsbeitrag
news:Sea2-F23utJLtosJ3Ih000001f3 / hotmail.com...
>
> Hi everyone.
>
> I'm trying to learn how to program in Ruby, and yesterday I came across
a
> Regexp issue I can't figure out.  I want to parse a commandline argument
of
> the form: max_repeats=<integer>
>
> After studying the 'pick-axe' book, I came up with the line:
> regexp = Regexp.new("\Amax_repeats=(\d+)")

You're running into a quoting issue here.  See the subtle differences:

irb(main):003:0> Regexp.new("\Amax_repeats=(\d+)")
=> /Amax_repeats=(d+)/
irb(main):004:0> Regexp.new("\\Amax_repeats=(\\d+)")
=> /\Amax_repeats=(\d+)/
irb(main):005:0> %r(\Amax_repeats=(\d+))
=> /\Amax_repeats=(\d+)/
irb(main):006:0> /\Amax_repeats=(\d+)/
=> /\Amax_repeats=(\d+)/
irb(main):007:0> Regexp.new('\Amax_repeats=(\d+)')
=> /\Amax_repeats=(\d+)/

Your code (the first line) looses the backslash ("\") because it is the
escape char in the double quoted string.  You can use single quoted string
(last line) but in this case I'd prefer any of the other methods since the
string is constant anyway.

> but the above regexp fails to match with correct input.  When I changed
that
> line to the following:
> regexp = Regexp.new("^max_repeats=([0-9]+)")
>
> it *does* match correctly with the proper input.
>
> What I would like to know is, why doesn't the first Regexp work?  (FWIW,
I'm
> using Ruby 1.8.1 on Win2003 server.)

As said, the regexps are not near to identical.  Your expression really
starts with an uppercase "a" where you wanted it to start with "\A" which
is the "start of string" anchor.

Kind regards

    robert