Skip to content

Instantly share code, notes, and snippets.

@eljojo
Created April 9, 2013 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eljojo/5347842 to your computer and use it in GitHub Desktop.
Save eljojo/5347842 to your computer and use it in GitHub Desktop.
Replacing backreferences in Regular Expressions with its matched content.
require 'pp'
text = 'and the day is 08/11/1993 of course'
regex = 'the (day) (.+) <date> of'
result = regex.dup
until (s ||= 0) >= result.length
break unless section = (/(\((?!\?)(?:[^\(]+?[^\\]\)))/).match(result, s)
matches = Regexp.new(result.gsub(/<.+?>/, '.+?')).match(text)
offset = section.offset(0)
section = section[0]
result = result[0..offset.first - 1] + matches[1] + result[offset.last..result.length]
s = 0
end
puts "text: #{text}" # text: and the day is 08/11/1993 of course
puts "regex: #{regex}" # regex: the (day) (.+) <date> of
puts "result: #{result}" # result: the day is <date> of
@eljojo
Copy link
Author

eljojo commented Apr 9, 2013

The idea of this is to create a template system for regular expressions.
We have the regular expression the (day) (.+) <date> of and we want to execute it against and the day is 08/11/1993 of course, we want it to ignore the 08/11/1993 part and then we want to get a modified version of the regular expression that contains the matched content of the regular expression: the day is <date> of.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment