# Regex: greedy pattern

**URL:** <https://www.ruby-forum.com/t/regex-greedy-pattern/49846>\
**Category:** Ruby\
**Created:** [December 9, 2005, 5:38am UTC](https://www.ruby-forum.com/t/regex-greedy-pattern/49846 "2005-12-09T05:38:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![basi\_lio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@basi\_lio](https://www.ruby-forum.com/u/basi_lio)\
**Post date:** [December 9, 2005, 5:38am UTC](https://www.ruby-forum.com/t/regex-greedy-pattern/49846/1 "2005-12-09T05:38:42Z")

</div>

Hello,

In the code below, the pattern /#{a}/ consumes more than what I’d  
expect it to:

irb(main):338:0\> abbr = %w[Mr. Dr. i.e. Prof.]  
text = “Mr. Drake and Dr. Hide, i.e., Mr. Dride, I presume?”  
abbr.each do |a|  
abbrNoDot = a.gsub(/./,"")  
text.gsub!(/#{a}/,abbrNoDot)  
end  
puts text  
=\> [“Mr.”, “Dr.”, “ave.”, “st.”, “i.e.”, “Prof.”]  
=\> “Mr. Drake and Dr. Hide, i.e., Mr. Dride, I presume?”  
=\> [“Mr.”, “Dr.”, “ave.”, “st.”, “i.e.”, “Prof.”]  
Mr Drke and Dr Hie ie, Mr Drde, I presume?

Thus “Drake” and "Dride satisfy the pattern “Dr”, becoming “Drke” and  
“Drde”. I’ve tried many variations on the pattern /#{a}/, but I just  
don’t have enough Regex in my mind yet.

Thanks for the help,  
basi

---

<div class="post-metadata">

**Author:** ![basi\_lio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@basi\_lio](https://www.ruby-forum.com/u/basi_lio)\
**Post date:** [December 9, 2005, 5:54am UTC](https://www.ruby-forum.com/t/regex-greedy-pattern/49846/2 "2005-12-09T05:54:54Z")

</div>

On Dec 8, 2005, at 11:37 PM, basi wrote:

> end  
> Thanks for the help,  
> basi

/Dr\b/

---

<div class="post-metadata">

**Author:** ![basi\_lio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@basi\_lio](https://www.ruby-forum.com/u/basi_lio)\
**Post date:** [December 9, 2005, 6:03am UTC](https://www.ruby-forum.com/t/regex-greedy-pattern/49846/3 "2005-12-09T06:03:03Z")

</div>

basi wrote:

> end  
> Thanks for the help,  
> basi

You need to escape the dot which is matching any character.

abbr = %w[Mr. Dr. i.e. Prof.]  
text = “Mr. Drake and Dr. Hide, i.e., Mr. Dride, I presume?”  
abbr.each do |a|  
text.gsub!(/#{Regexp.escape(a)}/, a.gsub(/./, ‘’))  
end  
puts text  
#=\> Mr Drake and Dr Hide, ie, Mr Dride, I presume?

daz

---

<div class="post-metadata">

**Author:** ![basi\_lio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@basi\_lio](https://www.ruby-forum.com/u/basi_lio)\
**Post date:** [December 9, 2005, 6:11am UTC](https://www.ruby-forum.com/t/regex-greedy-pattern/49846/4 "2005-12-09T06:11:10Z")

</div>

Hi,

At Fri, 9 Dec 2005 13:37:35 +0900,  
basi wrote in [ruby-talk:169793]:

> Thus “Drake” and "Dride satisfy the pattern “Dr”, becoming “Drke” and  
> “Drde”. I’ve tried many variations on the pattern /#{a}/, but I just  
> don’t have enough Regex in my mind yet.

It’s not caused by greediness. You have to escape Regexp meta  
characters.

/#{Regexp.quote(a)}/

---

<div class="post-metadata">

**Author:** ![basi\_lio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@basi\_lio](https://www.ruby-forum.com/u/basi_lio)\
**Post date:** [December 9, 2005, 7:40am UTC](https://www.ruby-forum.com/t/regex-greedy-pattern/49846/5 "2005-12-09T07:40:45Z")

</div>

It works!  
So a metacharacter inside a pattern that is referenced by a variable is  
still a metacharacter. How obvious it is now that it’s been pointed out  
to me.

Thanks to all who responded,  
basi
