# How do I use a variable in a regexp search string?

**URL:** <https://rubytalk.org/t/how-do-i-use-a-variable-in-a-regexp-search-string/57197>\
**Category:** ruby-talk\
**Created:** [30 January 2010 00:50 UTC](https://rubytalk.org/t/how-do-i-use-a-variable-in-a-regexp-search-string/57197 "2010-01-30T00:50:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![joemac](https://avatars.discourse-cdn.com/v4/letter/j/48db29/32.png) [@joemac](https://rubytalk.org/u/joemac)\
**Post date:** [30 January 2010 00:50 UTC](https://rubytalk.org/t/how-do-i-use-a-variable-in-a-regexp-search-string/57197/1 "2010-01-30T00:50:44Z")

</div>

Hi All,

I dont know how to use a variable in a regex searchstring.  
ruby seems to only handle hardcoded serachstring in  
regex's. How do you do this. My code is below.

Also - how do you put a variable name in a  
File.open("filename.txt") statement like this:  
File.open(filevariable) where filevariable can  
be set to anything? I could not find this in  
the docs.

Thanks, --Joe  
================ snip code ===============  
#!/usr/bin/ruby

F = "filename.txt"  
searchstring = ARGV[0]

print "searchstring is ", searchstring, "\n"

File.open("filename.txt").each { |line|  
# this fails puts line if line =~ searchstring  
# this fails puts line if line =~ /searchstring/  
# this fails puts line if line =~ "searchstring"

#this works  
# puts line  
}

---

<div class="post-metadata">

**Author:** ![Harry3](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@Harry3](https://rubytalk.org/u/Harry3)\
**Post date:** [30 January 2010 03:38 UTC](https://rubytalk.org/t/how-do-i-use-a-variable-in-a-regexp-search-string/57197/2 "2010-01-30T03:38:31Z")

</div>

> I dont know how to use a variable in a regex searchstring.  
> ruby seems to only handle hardcoded serachstring in  
> regex's. How do you do this. My code is below.

Does this help?

str = "abcdef"  
searchstring = "cd"  
p str =~ /#{searchstring}/

Harry

---

<div class="post-metadata">

**Author:** ![Josh\_Cheek](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@Josh\_Cheek](https://rubytalk.org/u/Josh_Cheek)\
**Post date:** [30 January 2010 16:41 UTC](https://rubytalk.org/t/how-do-i-use-a-variable-in-a-regexp-search-string/57197/3 "2010-01-30T16:41:00Z")

</div>

This should work, here is an image showing how to use it:  
[http://tinypic.com/r/b5pi7m/6](http://tinypic.com/r/b5pi7m/6)

filename = 'source.txt'  
to\_find = Regexp.new ARGV[0]

puts "The Regexp is: #{to\_find.inspect}"

File.open(filename).each do |line|  
&nbsp;&nbsp;if line =~ to\_find  
&nbsp;&nbsp;&nbsp;&nbsp;puts "This line matches: #{line}"  
&nbsp;&nbsp;&nbsp;&nbsp;puts "This is what matches: #{line[to\_find]}"  
&nbsp;&nbsp;end  
end

> **···**
>
> On Fri, Jan 29, 2010 at 6:50 PM, joemac \<joemacbusiness@gmail.com\> wrote:
> 
> > Hi All,
> > 
> > I dont know how to use a variable in a regex searchstring.  
> > ruby seems to only handle hardcoded serachstring in  
> > regex's. How do you do this. My code is below.
> > 
> > Also - how do you put a variable name in a  
> > File.open("filename.txt") statement like this:  
> > File.open(filevariable) where filevariable can  
> > be set to anything? I could not find this in  
> > the docs.
> > 
> > Thanks, --Joe  
> > ================ snip code ===============  
> > #!/usr/bin/ruby
> > 
> > F = "filename.txt"  
> > searchstring = ARGV[0]
> > 
> > print "searchstring is ", searchstring, "\n"
> > 
> > File.open("filename.txt").each { |line|  
> > # this fails puts line if line =~ searchstring  
> > # this fails puts line if line =~ /searchstring/  
> > # this fails puts line if line =~ "searchstring"
> > 
> > #this works  
> > # puts line  
> > }

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [30 January 2010 03:56 UTC](https://rubytalk.org/t/how-do-i-use-a-variable-in-a-regexp-search-string/57197/4 "2010-01-30T03:56:57Z")

</div>

You may need to escape:

&nbsp;&nbsp;p str =~ /#{Regexp.escape(searchstring)}/

> **···**
>
> On Jan 29, 10:38 pm, Harry Kakueki \<list.p...@gmail.com\> wrote:
> 
> > \> I dont know how to use a variable in a regex searchstring.  
> > \> ruby seems to only handle hardcoded serachstring in  
> > \> regex's. How do you do this. My code is below.
> > 
> > Does this help?
> > 
> > str = "abcdef"  
> > searchstring = "cd"  
> > p str =~ /#{searchstring}/

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [30 January 2010 17:56 UTC](https://rubytalk.org/t/how-do-i-use-a-variable-in-a-regexp-search-string/57197/5 "2010-01-30T17:56:18Z")

</div>

Josh Cheek wrote:

> File.open(filename).each do |line|  
> &nbsp;&nbsp;if line =~ to\_find  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "This line matches: #{line}"  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "This is what matches: #{line[to\_find]}"

or to avoid having to match it twice:

&nbsp;&nbsp;puts "This is what matches: #{$&}"

Also:  
&nbsp;&nbsp;puts "This is what came before: #{$`}"  
&nbsp;&nbsp;puts "This is what came after: #{$'}"  
&nbsp;&nbsp;puts "Capture 1 is #{$1}"  
&nbsp;&nbsp;# etc

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![joemac](https://avatars.discourse-cdn.com/v4/letter/j/48db29/32.png) [@joemac](https://rubytalk.org/u/joemac)\
**Post date:** [22 February 2010 18:45 UTC](https://rubytalk.org/t/how-do-i-use-a-variable-in-a-regexp-search-string/57197/6 "2010-02-22T18:45:17Z")

</div>

Hi Folks,

Thanks for all the helpful info.  
It was the #{} syntax that i was missing.  
This also works for the "filename as a varible" situation  
when parsing lots of files:

filenames = ["input1", "input2", "input3"]

for fname in filenames  
&nbsp;&nbsp;&nbsp;infile = File.open("#{fname}")  
&nbsp;&nbsp;&nbsp;while line = infile.gets()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts line  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;puts "================== end file ==================="  
end

Very helpfull!

Thanks, --JM

> **···**
>
> On Jan 29, 7:38 pm, Harry Kakueki \<list.p...@gmail.com\> wrote:
> 
> > \> I dont know how to use avariablein a regex searchstring.  
> > \> ruby seems to only handle hardcoded serachstring in  
> > \> regex's. How do you do this. My code is below.
> > 
> > Does this help?
> > 
> > str = "abcdef"  
> > searchstring = "cd"  
> > p str =~ /#{searchstring}/
> > 
> > Harry

---

<div class="post-metadata">

**Author:** ![Marnen\_Laibow-Koser](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@Marnen\_Laibow-Koser](https://rubytalk.org/u/Marnen_Laibow-Koser)\
**Post date:** [30 January 2010 13:52 UTC](https://rubytalk.org/t/how-do-i-use-a-variable-in-a-regexp-search-string/57197/7 "2010-01-30T13:52:10Z")

</div>

Thomas Sawyer wrote:

> **···**
>
> > On Jan 29, 10:38�pm, Harry Kakueki \<list.p...@gmail.com\> wrote:
> > 
> > > \> I dont know how to use a variable in a regex searchstring.  
> > > \> ruby seems to only handle hardcoded serachstring in  
> > > \> regex's. � How do you do this. � My code is below.
> > > 
> > > Does this help?
> > > 
> > > str = "abcdef"  
> > > searchstring = "cd"  
> > > p str =~ /#{searchstring}/
> > 
> > You may need to escape:
> > 
> > &nbsp;&nbsp;p str =~ /#{Regexp.escape(searchstring)}/
> 
> But note that #{} only makes sense if you're interpolating a variable  
> into part of a regex -- say, /January #{year}/. If the variable is the  
> whole regex, then it's more sensible to do Regex.new(searchstring).
> 
> Best,  
> --  
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> marnen@marnen.org  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [22 February 2010 19:22 UTC](https://rubytalk.org/t/how-do-i-use-a-variable-in-a-regexp-search-string/57197/8 "2010-02-22T19:22:26Z")

</div>

In this case, you're already getting strings in fname so "#{fname}" isn't really doing anything useful.

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Feb 22, 2010, at 1:45 PM, joemac wrote:
> 
> > On Jan 29, 7:38 pm, Harry Kakueki \<list.p...@gmail.com\> wrote:
> > 
> > > > I dont know how to use avariablein a regex searchstring.  
> > > > ruby seems to only handle hardcoded serachstring in  
> > > > regex's. How do you do this. My code is below.
> > > 
> > > Does this help?
> > > 
> > > str = "abcdef"  
> > > searchstring = "cd"  
> > > p str =~ /#{searchstring}/
> > > 
> > > Harry
> > 
> > Hi Folks,
> > 
> > Thanks for all the helpful info.  
> > It was the #{} syntax that i was missing.  
> > This also works for the "filename as a varible" situation  
> > when parsing lots of files:
> > 
> > filenames = ["input1", "input2", "input3"]
> > 
> > for fname in filenames  
> > &nbsp;&nbsp;infile = File.open("#{fname}")  
> > &nbsp;&nbsp;while line = infile.gets()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts line  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;puts "================== end file ==================="  
> > end
> > 
> > Very helpfull!
> > 
> > Thanks, --JM
