Why is regexp "\Amax_repeats=(\d+)" not equal to "^max_repeats=([0-9]+)" ?

Discussion in 'Ruby' started by Martin Elzen, Apr 30, 2004.

  1. Martin Elzen

    Martin Elzen Guest

    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+)")

    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.)

    Sincerely,
    Martin

    PS a complete program with my regexp matching code is as follows:

    #define globals

    $max_repeat_count_default = 5




    def get_max_repeats_value
    result = $max_repeat_count_default

    if not ARGV.empty?

    #regexp = Regexp.new("\Amax_repeats=(\d+)")
    #@@@
    regexp = Regexp.new("^max_repeats=([0-9]+)")

    matchdata = candidate_val = match_val = nil

    ARGV.each do |arg|
    matchdata = regexp.match(arg)

    if matchdata != nil
    match_val = matchdata[1].to_i

    if candidate_val == nil
    candidate_val = match_val
    else
    candidate_val = match_val if match_val > candidate_val
    end

    end

    end

    result = candidate_val if candidate_val != nil and candidate_val > 0

    end

    result
    end



    begin
    max_repeats = get_max_repeats_value
    puts "max_repeats=" + max_repeats.to_s

    rescue Exception => ex
    print "Exception exception: " + ex.message +
    ",\nbacktrace:\n" + ex.backtrace.join("\n")

    end

    _________________________________________________________________
    Hotmail en Messenger on the move
    http://www.msn.nl/communicatie/smsdiensten/hotmailsmsv2/
     
    Martin Elzen, Apr 30, 2004
    #1
    1. Advertising

  2. Re: Why is regexp "\Amax_repeats=(\d+)" not equal to"^max_repeats=([0-9]+)" ?

    "Martin Elzen" <> wrote:
    > 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+)")
    >
    > 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.
    >


    Watch out about escaping inside double quoted strings..
    observe the difference

    irb(main):001:0> re = Regexp.new("\Ax")
    => /Ax/
    irb(main):002:0> re.match("xxx").to_a
    => []
    irb(main):003:0> re = Regexp.new('\Ax')
    => /\Ax/
    irb(main):004:0> re.match("xxx").to_a
    => ["x"]
    irb(main):005:0>


    Does this help you ?

    --
    Simon Strandgaard
     
    Simon Strandgaard, Apr 30, 2004
    #2
    1. Advertising

  3. Martin Elzen

    ts Guest

    >>>>> "M" == Martin Elzen <> writes:

    M> regexp = Regexp.new("\Amax_repeats=(\d+)")
    ^^ ^^

    You want \\ rather than \

    svg% ruby -e 'p Regexp.new("\Amax_repeats=(\d+)")'
    /Amax_repeats=(d+)/
    svg%

    svg% ruby -e 'p Regexp.new("\\Amax_repeats=(\\d+)")'
    /\Amax_repeats=(\d+)/
    svg%

    ou use simple quote, i.e. ''


    Guy Decoux
     
    ts, Apr 30, 2004
    #3
  4. "Martin Elzen" <> schrieb im Newsbeitrag
    news:...
    >
    > 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
     
    Robert Klemme, Apr 30, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    909
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,998
    Smokey Grindel
    Dec 2, 2006
  3. Red Orchid

    About JTextPane, Why "Not equal" ?

    Red Orchid, Mar 31, 2007, in forum: Java
    Replies:
    14
    Views:
    652
    Andrew Thompson
    Mar 31, 2007
  4. Joao Silva
    Replies:
    16
    Views:
    363
    7stud --
    Aug 21, 2009
  5. Harry
    Replies:
    12
    Views:
    295
    Simon Krahnke
    Sep 23, 2009
Loading...

Share This Page