Regex search string as is without interpolation

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

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.

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

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 :slight_smile:

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