Created
April 6, 2012 18:38
Perl to Ruby Regular Expression Conversion
This file contains 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
POSSIBLE_OPTIONS = "[misxp]" | |
def rubify_regexp(l) | |
re = l.gsub(/\(\?(#{POSSIBLE_OPTIONS}*)(?:-(#{POSSIBLE_OPTIONS}*))?:/) do |_| | |
enabled = $1 | |
disabled = $2 | |
# Perl's `s` option is `m` in Ruby | |
if enabled.include?('s') && !enabled.include?('m') | |
enabled.sub!('s', 'm') | |
else | |
enabled.sub!('s', '') | |
end | |
# If Perl wanted to disable `s` then we need to disable `m` | |
if disabled && disabled.include?('s') | |
enabled.sub!('m', '') | |
end | |
"(?#{enabled}:" | |
end | |
Regexp.new(re) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment