# Using a variable to build a regex

**URL:** <https://www.ruby-forum.com/t/using-a-variable-to-build-a-regex/123988>\
**Category:** Ruby\
**Created:** [December 23, 2007, 4:19am UTC](https://www.ruby-forum.com/t/using-a-variable-to-build-a-regex/123988 "2007-12-23T04:19:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dirtytree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@dirtytree](https://www.ruby-forum.com/u/dirtytree)\
**Post date:** [December 23, 2007, 4:19am UTC](https://www.ruby-forum.com/t/using-a-variable-to-build-a-regex/123988/1 "2007-12-23T04:19:26Z")

</div>

I need to search a roster file (plain text) to match for a player name.  
I  
tried this:

dthacker@buckbeak:~/learning/ruby$ irb  
irb(main):001:0\> player=“S\_Avrul”  
=\> “S\_Avrul”  
irb(main):002:0\> player\_line=“S\_Avrul ger 25 R 1 2 3 7 40 35”  
=\> “S\_Avrul ger 25 R 1 2 3 7 40 35”  
irb(main):003:0\> re=/player/  
=\> /player/  
irb(main):004:0\> re.match(player\_line)  
=\> nil  
irb(main):005:0\>

How can I use the value of “player” to create the regex?

TIA Dave

---

<div class="post-metadata">

**Author:** ![dirtytree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@dirtytree](https://www.ruby-forum.com/u/dirtytree)\
**Post date:** [December 23, 2007, 4:25am UTC](https://www.ruby-forum.com/t/using-a-variable-to-build-a-regex/123988/2 "2007-12-23T04:25:16Z")

</div>

Dave T. wrote:

> irb(main):004:0\> re.match(player\_line)  
> =\> nil  
> irb(main):005:0\>
> 
> How can I use the value of “player” to create the regex?
> 
> TIA Dave

/#{player}/

---

<div class="post-metadata">

**Author:** ![dirtytree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@dirtytree](https://www.ruby-forum.com/u/dirtytree)\
**Post date:** [December 23, 2007, 4:30am UTC](https://www.ruby-forum.com/t/using-a-variable-to-build-a-regex/123988/3 "2007-12-23T04:30:32Z")

</div>

On Dec 22, 8:18 pm, Dave T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How can I use the value of “player” to create the regex?

In a string literal, “player” would also be the actual string “player”  
and not the value of that variable. Do you know how to substitute  
inside a string? The same answer applies to Regexp literals in Ruby.

For a hint:  
[http://phrogz.net/ProgrammingRuby/language.html#substitutions](http://phrogz.net/ProgrammingRuby/language.html#substitutions)

(In

---

<div class="post-metadata">

**Author:** ![dirtytree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@dirtytree](https://www.ruby-forum.com/u/dirtytree)\
**Post date:** [December 23, 2007, 5:40am UTC](https://www.ruby-forum.com/t/using-a-variable-to-build-a-regex/123988/4 "2007-12-23T05:40:41Z")

</div>

Dave T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> irb(main):004:0\> re.match(player\_line)  
> =\> nil  
> irb(main):005:0\>
> 
> How can I use the value of “player” to create the regex?

You can use #{} substitution in a Regexp, the same way you can in a  
string. Thus re=/#{player}/. You can also use Regexp.new to construct  
a regexp from a string. (So you can do ordinary string manipulations  
to construct the regexp as a string, then turn that into a regexp.)

Both of these will treat any metacharacters in the string as  
metacharacters (e.g. A “.” in the string will match any character). To  
avoid this problem, use Regexp.escape.

–Ken
