# Regexp match problem

**URL:** <https://www.ruby-forum.com/t/regexp-match-problem/90639>\
**Category:** Rails\
**Created:** [March 30, 2007, 4:46pm UTC](https://www.ruby-forum.com/t/regexp-match-problem/90639 "2007-03-30T16:46:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [March 30, 2007, 4:46pm UTC](https://www.ruby-forum.com/t/regexp-match-problem/90639/1 "2007-03-30T16:46:05Z")

</div>

Why doesn’t this return true when I do the “match” method?

irb\> @html\_code = @joe  
=\> "Are you in college and looking for \<a  
href=‘[http://www.UniversityRenter.com](http://www.UniversityRenter.com)’

> apartments? Well then UniversityRenter is the place to go for college apartments!"

irb\> @joe == @html\_code  
=\> true

irb\> @joe.match(@html\_code)  
=\> nil

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [March 30, 2007, 6:07pm UTC](https://www.ruby-forum.com/t/regexp-match-problem/90639/2 "2007-03-30T18:07:11Z")

</div>

On Mar 30, 2007, at 10:46 AM, Joe P. wrote:

> =\> true
> 
> irb\> @joe.match(@html\_code)  
> =\> nil

because the ‘?’ in the string is a metacharacter so when turned into  
a Regexp by String#match(pattern), it doesn’t match ‘itself’

Try @joe.match(Regexp.escape(@html\_code))

Or just look at how Regexp.escape(@joe) differs from @joe

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [March 30, 2007, 6:12pm UTC](https://www.ruby-forum.com/t/regexp-match-problem/90639/3 "2007-03-30T18:12:21Z")

</div>

Rob B. wrote:

> On Mar 30, 2007, at 10:46 AM, Joe P. wrote:
> 
> > =\> true
> > 
> > irb\> @joe.match(@html\_code)  
> > =\> nil
> 
> because the ‘?’ in the string is a metacharacter so when turned into  
> a Regexp by String#match(pattern), it doesn’t match ‘itself’
> 
> Try @joe.match(Regexp.escape(@html\_code))
> 
> Or just look at how Regexp.escape(@joe) differs from @joe
> 
> -Rob
> 
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

Thanks Rob.

I ended up just doing @joe.include?(@html\_code), which seems to be  
working.
