# Variable substitution in a regex

**URL:** <https://rubytalk.org/t/variable-substitution-in-a-regex/18485>\
**Category:** ruby-talk\
**Created:** [18 May 2005 12:53 UTC](https://rubytalk.org/t/variable-substitution-in-a-regex/18485 "2005-05-18T12:53:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Callaway](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@Steve\_Callaway](https://rubytalk.org/u/Steve_Callaway)\
**Post date:** [18 May 2005 12:53 UTC](https://rubytalk.org/t/variable-substitution-in-a-regex/18485/1 "2005-05-18T12:53:07Z")

</div>

Yeah, straightforward enough, moreover you can process  
an array of vars as in the snippet I put up on  
Rubyforge:

# function to regexp vars  
def is\_element\_in\_string?(in\_string, in\_array)  
in\_array.each do |line|  
rx\_test = /#{line}/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if in\_string =~ rx\_test  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
end  
return false  
end

# as an example we may wish to see whether a list of  
football match results  
# contains a result of a team we are interested in  
varray = Array.new  
# now populate the array with reegxps you wish to  
match...  
varray = ["Plymouth Argyle", "Blackburn Rovers" ,  
"Manchester .\*", "Tott.\*"]

#Examples:  
results = "Plymouth Argyle 5 - Cardiff 0"  
puts is\_element\_in\_string?(results, varray)  
#=\> true

results = "Watford 3 - Sunderland 0"  
puts is\_element\_in\_string?(results, varray)  
#=\> false, no match

results = "Man City 2 - Man Utd 0"  
puts is\_element\_in\_string?(results, varray)  
#=\> false  
# since it obviously won't regexp unless we add "Man  
U.\*" to our array...  
# e.g.  
varray.push("Man U.\*")  
puts is\_element\_in\_string?(results, varray)  
# =\> true

> **···**
>
> --- Scott Pack \<softwarespack@yahoo.com\> wrote:
> 
> > Hi,
> > 
> > I am new to Ruby. Can I substitute the value of a  
> > variable into a regular expression? For example:
> > 
> > exp = /(\d+)/ #works  
> > exp = /(\d+)/ + 'some string value' #Not!
> > 
> > Thanks,
> > 
> > S
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > Yahoo! Mail Mobile  
> > Take Yahoo! Mail with you! Check email on your  
> > mobile phone.  
> > [http://mobile.yahoo.com/learn/mail](http://mobile.yahoo.com/learn/mail)
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Do you Yahoo!?  
> Yahoo! Small Business - Try our new resources site!
> 
> > **[Turbify.com | Buy a Domain | Websites & Domains | Purchase a URL - Turbify](https://www.turbify.com)**
> >
> > Turbify is a trusted internet domain registrar headquarter in Rockville, Maryland offering Domain Name, Web Hosting, Business Email, Website Builder, Local Business Listing and eCommerce Store services.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [18 May 2005 13:19 UTC](https://rubytalk.org/t/variable-substitution-in-a-regex/18485/2 "2005-05-18T13:19:13Z")

</div>

Hi --

> Yeah, straightforward enough, moreover you can process  
> an array of vars as in the snippet I put up on  
> Rubyforge:
> 
> # function to regexp vars  
> def is\_element\_in\_string?(in\_string, in\_array)  
> in\_array.each do |line|  
> rx\_test = /#{line}/  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if in\_string =~ rx\_test  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> end  
> return false  
> end

Let Ruby do the work for you 🙂

&nbsp;&nbsp;&nbsp;def is\_element\_in\_string?(str, elements)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elements.any? {|e| /#{e}/.match(str) }  
&nbsp;&nbsp;&nbsp;end

> # as an example we may wish to see whether a list of  
> football match results  
> # contains a result of a team we are interested in  
> varray = Array.new  
> # now populate the array with reegxps you wish to  
> match...  
> varray = ["Plymouth Argyle", "Blackburn Rovers" ,  
> "Manchester .\*", "Tott.\*"]

You're not populating the array; you're discarding the array and  
creating a completely new one 🙂 The array you created previously  
ceases to exist.

You can either just leave out that first assignment, or if you have  
some reason to create and populate the array in two steps, you could  
do:

&nbsp;&nbsp;&nbsp;varray = Array.new  
&nbsp;&nbsp;&nbsp;varray.replace(["Plymouth ...", "...", ...])

but I can't think of any reason to do that. I would just assign the  
array to the variable, as in your second assignment.

David

> **···**
>
> On Wed, 18 May 2005, Steve Callaway wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net
