tags:

views:

50

answers:

3

This is one of the most difficult things to search for because when you search how to parse a regex you get how to parse with a regex. I want to read a regex from a file, put it in an object somehow, then parse strings using that regex in my program.

Im sure someone has done this before, can you help me out on where to start?

+2  A: 

You can use Regexp.new to create a regex from a string. So if your file contains only the regex you can do:

regex = Regexp.new( File.read("somefile").chomp )

(chomp to remove the trailing newline if there is one, assuming you don't want that in the regex)

sepp2k
+2  A: 
r = /.ol/
File.open('test.txt','w') { |f| f.print r }
r = Regexp.new IO.read 'test.txt'
puts %w{rofl lol test lol2}.grep r
Nakilon
Hm... SO's syntax highlighting doesn't know class `IO`?
Nakilon
A: 

YAML for ruby supports regexes.

Andrew Grimm