Skip to content

Instantly share code, notes, and snippets.

@h3h
Created April 6, 2012 18:38
Show Gist options
  • Save h3h/2321978 to your computer and use it in GitHub Desktop.
Save h3h/2321978 to your computer and use it in GitHub Desktop.
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