Created
April 9, 2013 17:53
-
-
Save eljojo/5347842 to your computer and use it in GitHub Desktop.
Replacing backreferences in Regular Expressions with its matched content.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 againstand the day is 08/11/1993 of course
, we want it to ignore the08/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
.