From: "Wolfgang Nádasi-Donner" Date: 2007-02-15T03:10:05+09:00 Subject: Re: Programmatically turning a Regexp into an anchored Regexp Greg Hurrell schrieb: > Is there any programmatic way to take a Regexp like /foo/ and turn it > into an anchored Regexp (/^foo/)? I'm looking for a programmatic way > to do this because the actual Regexp is dynamic, not known until run > time. > > I'm wishing for an option or method similar to the EXTENDED, > IGNORECASE, MULTILINE options, except for anchoring. > > I suspect the answer will be "no", but I wanted to ask anyway before > giving up hope... > > Cheers, > Greg > irb(main):001:0> r1 = /foo/ => /foo/ irb(main):002:0> "the foo in the middle".gsub(r1, '###') => "the ### in the middle" irb(main):003:0> "foo at the beginning".gsub(r1, '###') => "### at the beginning" irb(main):004:0> "foo at the beginning".gsub(/^#{r1}/, '###') => "### at the beginning" irb(main):005:0> "the foo in the middle".gsub(/^#{r1}/, '###') => "the foo in the middle" O.K.? Wolfgang N�dasi-Donner