Forum: Ruby Programmatically turning a Regexp into an anchored Regexp

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.
B73dde621deaf42324793d8517b0b78c?d=identicon&s=25 Greg Hurrell (Guest)
on 2007-02-14 18:51
(Received via mailing list)
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
A131b672fdbd2a58dce12031ad78b121?d=identicon&s=25 Wolfgang Nádasi-Donner (wonado)
on 2007-02-14 19:11
(Received via mailing list)
Greg Hurrell schrieb:
>
> 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
4d5b5dd4e263d780a5dfe7ac8b8ac98c?d=identicon&s=25 Tim Pease (Guest)
on 2007-02-14 19:34
(Received via mailing list)
On 2/14/07, Greg Hurrell <greg.hurrell@gmail.com> wrote:
> 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
B73dde621deaf42324793d8517b0b78c?d=identicon&s=25 Greg Hurrell (Guest)
on 2007-02-14 19:40
(Received via mailing list)
On 14 feb, 19:06, Wolfgang Nádasi-Donner <won...@donnerweb.de> wrote:
>
> 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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-02-14 19:57
(Received via mailing list)
On Feb 14, 2007, at 11:50 AM, Greg Hurrell wrote:

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

 >> class String
 >>   def matches_at_beginning?(regexp)
 >>     (self =~ regexp) == 0
 >>   end
 >> end
=> nil
 >> "abc".matches_at_beginning? /a/
=> true
 >> "abc".matches_at_beginning? /b/
=> false

And:

 >> require "strscan"
=> true
 >> scanner = StringScanner.new("abc")
=> #<StringScanner 0/3 @ "abc">
 >> scan
scan     scanner
 >> scanner.scan(/a/)
=> "a"
 >> sc
scan     scanner
 >> scanner.reset
=> #<StringScanner 0/3 @ "abc">
 >> scanner.scan(/b/)
=> nil

James Edward Gray II
This topic is locked and can not be replied to.