Match doesn't match

Discussion in 'Ruby' started by Volkan Civelek, Jul 19, 2006.

  1. arg1 = "(1)a"
    arg2 = "(1)a"

    if arg1.match(arg2)
    puts "Matched"
    else
    puts "Don't Matched"
    end


    gives me "Don't Matched" ??

    Regards,
    Volkan


    --
    Posted via http://www.ruby-forum.com/.
    Volkan Civelek, Jul 19, 2006
    #1
    1. Advertising

  2. arg2 should be escaped as () has a meaning in regexps.

    arg2 = "\\(1\\)a"

    p "match" if arg1.match(arg2)

    hth,

    /alex
    --
    w( the_mindstorm )p.


    On 7/19/06, Volkan Civelek <> wrote:
    >
    >
    > arg1 = "(1)a"
    > arg2 = "(1)a"
    >
    > if arg1.match(arg2)
    > puts "Matched"
    > else
    > puts "Don't Matched"
    > end
    >
    >
    > gives me "Don't Matched" ??
    >
    > Regards,
    > Volkan
    >
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    Alexandru Popescu, Jul 19, 2006
    #2
    1. Advertising

  3. Volkan Civelek

    Mike Stok Guest

    On 18-Jul-06, at 8:57 PM, Volkan Civelek wrote:

    >
    >
    > arg1 = "(1)a"
    > arg2 = "(1)a"
    >
    > if arg1.match(arg2)
    > puts "Matched"
    > else
    > puts "Don't Matched"
    > end
    >
    >
    > gives me "Don't Matched" ??


    That's because parentheses are special in a pattern, and that's what
    the String#match expects as an argument. You can use Regexp.escape
    to escape special characters:

    mike$ irb --prompt simple
    >> arg1 = "(1)a"

    => "(1)a"
    >> arg2 = "(1)a"

    => "(1)a"
    >> arg1.match(arg2)

    => nil
    >> arg1.match(Regexp.escape(arg2))

    => #<MatchData:0x34d6ac>

    Hope this helps,

    Mike

    --

    Mike Stok <>
    http://www.stok.ca/~mike/

    The "`Stok' disclaimers" apply.
    Mike Stok, Jul 19, 2006
    #3
  4. Try it with

    arg1 = "(1)a"
    arg2 = '\(1\)a'

    The problem is that arg2 is converted to a regular expression by
    "match" and "(" and ")" are special characters in regular expressions
    and, therefore, must be escaped.

    Regards, Morton

    On Jul 18, 2006, at 8:57 PM, Volkan Civelek wrote:

    > arg1 = "(1)a"
    > arg2 = "(1)a"
    >
    > if arg1.match(arg2)
    > puts "Matched"
    > else
    > puts "Don't Matched"
    > end
    >
    > gives me "Don't Matched" ??
    >
    > Regards,
    > Volkan
    Morton Goldberg, Jul 19, 2006
    #4
  5. Volkan Civelek

    Guest

    > arg2 should be escaped as () has a meaning in regexps.
    >
    > arg2 = "\\(1\\)a"
    >
    > p "match" if arg1.match(arg2)


    Possibly, Volkan Civelek, you simply wanted to see if the two string
    were equal? In that case you would do:

    arg1 = "(1)a"
    arg2 = "(1)a"

    if arg1 == arg2
    puts "Matched"
    else
    puts "Don't Matched"
    end

    Cheers,
    Benj
    , Jul 19, 2006
    #5
    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. hiwa
    Replies:
    0
    Views:
    635
  2. Victor
    Replies:
    2
    Views:
    637
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    363
    ekzept
    Aug 10, 2007
  4. John Gordon
    Replies:
    13
    Views:
    475
    Ian Kelly
    Dec 20, 2011
  5. Old Echo
    Replies:
    1
    Views:
    180
    Adam Shelly
    Sep 4, 2008
Loading...

Share This Page