Skip to content

Instantly share code, notes, and snippets.

Created December 16, 2016 10:43
Show Gist options
  • Save anonymous/d4f8bf6d9891ada3d5c10c402edfdb99 to your computer and use it in GitHub Desktop.
Save anonymous/d4f8bf6d9891ada3d5c10c402edfdb99 to your computer and use it in GitHub Desktop.
def extract_with_regex *p_arr
raise ArgumentError if p_arr.length < 1
result_s = self.dup
finds = p_arr.map do |rex|
result = {}
result_s.scan(rex) do
full_length = $&.length
start = $~.begin(0)
match = $~
result[start..start+full_length] = $~
end
result
end
intersection = Hash.new { |h,k| h[k] = [] }
finds.each_with_index do |h1,i1|
finds.each_with_index do |h2,i2|
if i1 < i2
h1.each do |segment1,x|
h2.each do |segment2,y|
if segment1.include? segment2.begin
intersection[i2] << segment2
end
end
end
end
end
end
intersection.each do |arr_idx,segments|
segments.each do |seg|
finds[arr_idx].delete(seg)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment