MrAutoIt MrAutoIt - 7 months ago 12
Ruby Question

How do you use Match/Regular Expression with a variable in Ruby?

This works:

match = string.match(/\A>.*\(12345\)/)
puts match


How do I do the same thing with a variable? Something like this:

number = 12345
match = string.match("/\A>.*\(#{number}\)/")
puts match

Answer

Regex literal already accepts interpolation. Just do

match = string.match(/\A>.*\(#{number}\)/)