Variable substitution in a regex

S

Scott Pack

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
 
J

James Edward Gray II

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!

Yes, the same way you interpolate in strings:

exp = /(\d+)#{'some string value'}/

Hope that helps.

James Edward Gray II
 
N

Nikolai Weibull

Scott said:
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!

exp = /(\d+)#{variable}/

but I don’t understand if that’s what you want. Your example doesn’t
agree with your question,
nikolai
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top