# Using eval() to create a Regexp object from a string

**URL:** <https://www.ruby-forum.com/t/using-eval-to-create-a-regexp-object-from-a-string/108557>\
**Category:** Ruby\
**Created:** [July 4, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/using-eval-to-create-a-regexp-object-from-a-string/108557 "2007-07-04T17:51:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [July 4, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/using-eval-to-create-a-regexp-object-from-a-string/108557/1 "2007-07-04T17:51:45Z")

</div>

Hi,

I’m trying to create a Regexp object by calling eval like this:

def eval\_to\_regexp(o)  
if o.is\_a?(Regexp)  
o  
elsif o.is\_a?(String)  
o = o[5…o.size] if o[0…4] == ‘eval:’  
eval("/#{o}/")  
else  
eval("/#{o.to\_s}/")  
end  
end

e = eval\_to\_regexp(“eval:some\_function() + ‘^’”)

Suppose some\_function() returns the string ‘/home/test’

And then I use it to test a match as follows:

‘/home/test’ =~ e # should return 0  
‘/home/test/somethingelse’ =~ e # should return nil (because of the ‘^’  
above)

I also tried using /#{o}/ directly instead of the eval, but it doesn’t  
work either ☹

Thanks in advance!

- Ivan V.

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [July 4, 2007, 5:59pm UTC](https://www.ruby-forum.com/t/using-eval-to-create-a-regexp-object-from-a-string/108557/2 "2007-07-04T17:59:28Z")

</div>

Ivan V. wrote:

> I’m trying to create a Regexp object by calling eval like this:

I’m sorry, but why don’t you just do regexp=Regexp.new(your\_string) ?

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [July 4, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/using-eval-to-create-a-regexp-object-from-a-string/108557/3 "2007-07-04T18:01:29Z")

</div>

Hi –

On Thu, 5 Jul 2007, Ivan V. wrote:

> else  
> ‘/home/test’ =~ e # should return 0  
> ‘/home/test/somethingelse’ =~ e # should return nil (because of the ‘^’  
> above)
> 
> I also tried using /#{o}/ directly instead of the eval, but it doesn’t  
> work either ☹

I think you might have better luck with Regexp.new:

e = Regexp.new("^#{some\_function}")

or something like that.

David

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [July 4, 2007, 6:05pm UTC](https://www.ruby-forum.com/t/using-eval-to-create-a-regexp-object-from-a-string/108557/4 "2007-07-04T18:05:04Z")

</div>

Sebastian H. wrote:

> Ivan V. wrote:
> 
> > I’m trying to create a Regexp object by calling eval like this:
> 
> I’m sorry, but why don’t you just do regexp=Regexp.new(your\_string) ?

Because I didn’t thought about that and I’m a complete newb regarding  
Ruby and Regexp (in general), as pointed out by David’s correction (the  
‘^’ is for the start of the string, not the end - thanks!).

Thanks a lot 🙂

- Ivan V.

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [July 4, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/using-eval-to-create-a-regexp-object-from-a-string/108557/5 "2007-07-04T18:45:34Z")

</div>

On Jul 4, 2007, at 10:51 AM, Ivan V. wrote:

> Suppose some\_function() returns the string ‘/home/test’

I see you already have your answer, but another thing you may want to  
be aware of is Regexp.escape():

def some\_function; ‘/home/test’ end

re = /#{Regexp.escape(some\_function)}/ # =\> //home/test/  
re.source # =\> “/home/test”

James Edward G. II

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [July 5, 2007, 8:06pm UTC](https://www.ruby-forum.com/t/using-eval-to-create-a-regexp-object-from-a-string/108557/6 "2007-07-05T20:06:56Z")

</div>

> > > I’m trying to create a Regexp object by calling eval like this:
> > 
> > I’m sorry, but why don’t you just do regexp=Regexp.new(your\_string) ?
> 
> Because I didn’t thought about that and I’m a complete newb regarding  
> Ruby and Regexp (in general), as pointed out by David’s correction (the  
> ‘^’ is for the start of the string, not the end - thanks!).

Be careful about eval()! I definitely enjoy eval() but you need to  
have some perspective. It’s not for newbs.

–  
Giles B.

Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)
