Forum: Ruby Regex: greedy pattern

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Acdabad15b58ba105df230e3ea02523d?d=identicon&s=25 basi_lio (Guest)
on 2005-12-09 05:38
(Received via mailing list)
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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 logancapaldo (Guest)
on 2005-12-09 05:54
(Received via mailing list)
On Dec 8, 2005, at 11:37 PM, basi wrote:

> end
> Thanks for the help,
> basi
>
>

/Dr\b/
Dce0999389d102f9a313af625375304c?d=identicon&s=25 dooby (Guest)
on 2005-12-09 06:03
(Received via mailing list)
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
04a56914cc09f0858d3fca2bf4cbde34?d=identicon&s=25 nobuyoshi.nakada (Guest)
on 2005-12-09 06:11
(Received via mailing list)
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)}/
Acdabad15b58ba105df230e3ea02523d?d=identicon&s=25 basi_lio (Guest)
on 2005-12-09 07:40
(Received via mailing list)
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
This topic is locked and can not be replied to.