Programmatically turning a Regexp into an anchored Regexp

Discussion in 'Ruby' started by Greg Hurrell, Feb 14, 2007.

  1. Greg Hurrell

    Greg Hurrell Guest

    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
     
    Greg Hurrell, Feb 14, 2007
    #1
    1. Advertising

  2. 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
     
    Wolfgang Nádasi-Donner, Feb 14, 2007
    #2
    1. Advertising

  3. Greg Hurrell

    Tim Pease Guest

    On 2/14/07, 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'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
     
    Tim Pease, Feb 14, 2007
    #3
  4. Greg Hurrell

    Greg Hurrell Guest

    On 14 feb, 19:06, Wolfgang Nádasi-Donner <> 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
     
    Greg Hurrell, Feb 14, 2007
    #4
  5. 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
     
    James Edward Gray II, Feb 14, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Randy
    Replies:
    1
    Views:
    473
    Steve C. Orr [MVP, MCSD]
    Sep 30, 2004
  2. .Net Sports

    Anchored links for aspx files

    .Net Sports, Jun 20, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    350
    .Net Sports
    Jun 20, 2005
  3. rnicz
    Replies:
    6
    Views:
    120
    rnicz
    Nov 19, 2004
  4. Joao Silva
    Replies:
    16
    Views:
    363
    7stud --
    Aug 21, 2009
  5. Replies:
    0
    Views:
    99
Loading...

Share This Page