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
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
arg2 should be escaped as () has a meaning in regexps.
arg2 = “\(1\)a”
p “match” if arg1.match(arg2)
hth,
.w( the_mindstorm )p.
On 18-Jul-06, at 8:57 PM, Volkan Civelek wrote:
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)
=> nilarg1.match(Regexp.escape(arg2))
=> #MatchData:0x34d6ac
Hope this helps,
Mike
–
Mike S. [email protected]
http://www.stok.ca/~mike/
The “`Stok’ disclaimers” apply.
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs