Skip to content

Instantly share code, notes, and snippets.

@jbr
Created September 20, 2009 03:12
Show Gist options
  • Save jbr/189693 to your computer and use it in GitHub Desktop.
Save jbr/189693 to your computer and use it in GitHub Desktop.
def extract_regex(string)
string.scan(/([a-z_]+):(?:\/|%r\{)((?:(?:\\\/)|[^\/])+)(?:\/|\})/).inject({}) do |hash, match|
hash.merge match.first.to_sym => Regexp.new(match.last)
end
end
test_string = 'this is a a:/regular/ expression b:/with a \/ in it/ and some c:%r{other stuff}'
expected = {:a => /regular/, :b => /with a \/ in it/, :c => /other stuff/}
actual = extract_regex(test_string)
if expected == actual
puts "success"
else
puts "fail (expected #{expected.inspect}, got #{actual.inspect})"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment