# Match doesn't match

**URL:** <https://www.ruby-forum.com/t/match-doesnt-match/98931>\
**Category:** Ruby\
**Created:** [July 19, 2006, 2:56am UTC](https://www.ruby-forum.com/t/match-doesnt-match/98931 "2006-07-19T02:56:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vcivelek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vcivelek](https://www.ruby-forum.com/u/vcivelek)\
**Post date:** [July 19, 2006, 2:56am UTC](https://www.ruby-forum.com/t/match-doesnt-match/98931/1 "2006-07-19T02:56:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vcivelek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vcivelek](https://www.ruby-forum.com/u/vcivelek)\
**Post date:** [July 19, 2006, 3:10am UTC](https://www.ruby-forum.com/t/match-doesnt-match/98931/2 "2006-07-19T03:10:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vcivelek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vcivelek](https://www.ruby-forum.com/u/vcivelek)\
**Post date:** [July 19, 2006, 3:16am UTC](https://www.ruby-forum.com/t/match-doesnt-match/98931/3 "2006-07-19T03:16:37Z")

</div>

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)  
> > =\> nil  
> > arg1.match(Regexp.escape(arg2))  
> > =\> #MatchData:0x34d6ac

Hope this helps,

Mike

–

Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)

The “`Stok’ disclaimers” apply.

---

<div class="post-metadata">

**Author:** ![vcivelek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vcivelek](https://www.ruby-forum.com/u/vcivelek)\
**Post date:** [July 19, 2006, 3:25am UTC](https://www.ruby-forum.com/t/match-doesnt-match/98931/4 "2006-07-19T03:25:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vcivelek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vcivelek](https://www.ruby-forum.com/u/vcivelek)\
**Post date:** [July 19, 2006, 9:46am UTC](https://www.ruby-forum.com/t/match-doesnt-match/98931/5 "2006-07-19T09:46:09Z")

</div>

> 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
