Skip to content

Instantly share code, notes, and snippets.

@caius
Created August 17, 2009 00:07
Show Gist options
  • Save caius/168814 to your computer and use it in GitHub Desktop.
Save caius/168814 to your computer and use it in GitHub Desktop.
require "extend_regex_concat"
a = /foo/
b = /bar/m
c = /(sed)/i
Regexp.concat(a, b, c) # => /foobar(sed)/mi
module RegexpConcatenation
#
# Concatenates Regexp objects together into a new Regexp object.
# Keeps any flags, and adds them together. <tt>//i + //m == //im</tt>
#
def concat *regexes
# We need somewhere to store the flags from each regex
@flags = ""
# Run through the regexes in a map!
regexes.map! do |regex|
# Separate out the flags and the actual regex itself
flags, str = regex.to_s.scan(/(?:^\(\?(m?i?x?)-?m?i?x?:(.*?)\)$)/).first
# Add the flags to our store
@flags += flags
# And map this element to just the regex str
str
end
# Run through the flags, strip any duplicates
# and then convert them into the numerical counterpart
@flags = @flags.split("").uniq.inject(0) do |sum, flag|
sum += case flag
when "i"
1
when "x"
2
when "m"
4
else
0
end
end
# Get rid of the instance variable
f = @flags; @flags = nil;
# And return a Regexp object created from the regex strings
# complete with any flags we need specified.
Regexp.new(regexes.join(""), f)
end
end
Regexp.send(:extend, RegexpConcatenation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment