# Programmatically turning a Regexp into an anchored Regexp

**URL:** <https://www.ruby-forum.com/t/programmatically-turning-a-regexp-into-an-anchored-regexp/85579>\
**Category:** Ruby\
**Created:** [February 14, 2007, 6:51pm UTC](https://www.ruby-forum.com/t/programmatically-turning-a-regexp-into-an-anchored-regexp/85579 "2007-02-14T18:51:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 14, 2007, 6:51pm UTC](https://www.ruby-forum.com/t/programmatically-turning-a-regexp-into-an-anchored-regexp/85579/1 "2007-02-14T18:51:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 14, 2007, 7:11pm UTC](https://www.ruby-forum.com/t/programmatically-turning-a-regexp-into-an-anchored-regexp/85579/2 "2007-02-14T19:11:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 14, 2007, 7:34pm UTC](https://www.ruby-forum.com/t/programmatically-turning-a-regexp-into-an-anchored-regexp/85579/3 "2007-02-14T19:34:16Z")

</div>

On 2/14/07, Greg H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 14, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/programmatically-turning-a-regexp-into-an-anchored-regexp/85579/4 "2007-02-14T19:57:20Z")

</div>

On Feb 14, 2007, at 11:50 AM, Greg H. 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 G. II

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 14, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/programmatically-turning-a-regexp-into-an-anchored-regexp/85579/5 "2007-02-14T19:40:18Z")

</div>

On 14 feb, 19:06, Wolfgang Nádasi-Donner [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
