Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Perl to Ruby Regular Expression Conversion
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
You can’t perform that action at this time.