Programmatically turning a Regexp into an anchored Regexp

G

Greg Hurrell

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
 
W

Wolfgang Nádasi-Donner

Greg said:
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
 
T

Tim Pease

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...


def anchor( rgxp )
Regexp.new "^#{rgxp.inspect[1...-1]}"
end

I would recommend using the '\A' anchor instead of the '^' anchor.
'^' matches at the beginning of a line so that "blah blah\nfoo" would
match. '\A' matches at the beginning of the string so that "blah
blah\nfoo" will not match, but "foo\nblah blah" will match.

def anchor( rgxp )
Regexp.new "\\A#{rgxp.inspect[1...-1]}"
end


Blessings,
TwP
 
G

Greg Hurrell

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"

Thanks, Wolfgang. I had no idea that #{} could be used not only to
interpolate within literal Strings, but within Regexps as well. I now
see that it's noted on page 66 of The Pickaxe... "In addition, a
regular expression may contain #{...} expression substitutions."

I tried it out and I see that it also works even if the original
Regexp already has a "^" in it...

Cheers,
Greg
 
J

James Edward Gray II

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 see you already have your answer, but just to throw other ideas
into the mix:
=> false

And:
=> nil

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top