# Regex search string as is without interpolation

**URL:** <https://www.ruby-forum.com/t/regex-search-string-as-is-without-interpolation/234504>\
**Category:** Ruby\
**Created:** [March 9, 2015, 9:47pm UTC](https://www.ruby-forum.com/t/regex-search-string-as-is-without-interpolation/234504 "2015-03-09T21:47:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [March 9, 2015, 9:47pm UTC](https://www.ruby-forum.com/t/regex-search-string-as-is-without-interpolation/234504/1 "2015-03-09T21:47:48Z")

</div>

Hi,

I am trying to use a regex search to find numbers in a string which  
contains string interpolation… I need to find and place the number  
between the brackets (ie 2) into a variable.

text = “Which corneal dystrophy is inherited in #{question\_items[2]}  
pattern?”  
puts text  
=\> Which corneal dystrophy is inherited in an autosomal dominant  
pattern?

if (/[[0-99]]/) =~ text #Search for [2]  
puts text  
end

=\> Does not find it!

Anybody know how to use regex to search the text abd see the string  
without interpolating it?

Thanks,

Dave C., MD

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2015, 9:55pm UTC](https://www.ruby-forum.com/t/regex-search-string-as-is-without-interpolation/234504/2 "2015-03-09T21:55:03Z")

</div>

Once it evaluates the variable “text” it has already done the  
interpolation.

“#{question\_items[2]}” is not there anymore.

If you want that string as plain text, you can use single quotes around  
it, which won’t evaluate the special characters.

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2015, 9:57pm UTC](https://www.ruby-forum.com/t/regex-search-string-as-is-without-interpolation/234504/3 "2015-03-09T21:57:18Z")

</div>

Dave C. wrote in post #1169881:

> if (/[[0-99]]/) =~ text #Search for [2]  
> puts text  
> end

if (/[([0-9]+)]/) =~ text #Search for [2]  
puts text  
puts $1  
end

>

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2015, 10:29pm UTC](https://www.ruby-forum.com/t/regex-search-string-as-is-without-interpolation/234504/4 "2015-03-09T22:29:17Z")

</div>

Regis d’Aubarede wrote in post #1169883:

> Dave C. wrote in post #1169881:

Single quotes work, but how to convert from:  
questions= [  
[[“Which corneal dystrophy is inherited in #{question\_items[2]}  
pattern?”],  
[“Which corneal dystrophy located in #{question\_items[1]} is inherited  
in #{question\_items[2]} pattern?”,“2”],  
[“Which corneal dystrophy located in #{question\_items[1]} is inherited  
in #{question\_items[2]} and has an onset in #{question\_items[3]}  
pattern?”,“2”] ]  
]

I am actually randomly selecting one of the lines in the array and  
searching it for all occurances of [any number] and then putting the  
numbers into a new array.  
Would getting the line from the array and replacing the double quotes  
with single quotes be an appropriate way of doing this?

Thanks - obviously this isn’t my regular job 🙂

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 9, 2015, 10:45pm UTC](https://www.ruby-forum.com/t/regex-search-string-as-is-without-interpolation/234504/5 "2015-03-09T22:45:37Z")

</div>

Regis d’Aubarede wrote in post #1169883:

> Dave C. wrote in post #1169881:
> 
> > if (/[[0-99]]/) =~ text #Search for [2]  
> > puts text  
> > end
> 
> if (/[([0-9]+)]/) =~ text #Search for [2]  
> puts text  
> puts $1  
> end
> 
> >

This does not work as per Joel P.'s response above.  
#{question\_items[2]} is not there as has already been evaluated.

I did change the format of my regex to your format though. Thank you!

Dave C.. MD
