Alex Rivera | Logout

Ruby: generating new Regexps from strings

Asked 2011-03-24T00:01:47.593
13

Is there any way to convert String to Regexp (in Ruby)? Let's say:

'example' ---> /example/

My purpose is generating Regexps dynamically.

Edit
Report

2 Answers

4

You can also write...

regex = Regexp.compile(string)

...which is a very descriptive name. This method compiles the source code (string) into a nondeterministic finite automaton (regex). The NFA can then be reused over and over.

answered 2011-03-24T19:28:26.573
2

you can try /#{your variable}/

answered 2011-03-24T00:03:28.293

Your Answer