Created
September 20, 2009 03:12
-
-
Save jbr/189693 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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