Extensions to the Regexp class, with some common and/or complex regular expressions.
We extend the Regexp class with an Irc module which will contain some Irc-specific regexps
First of all we add a method to the Regexp class
We start with some general-purpose ones which will be used in the Irc module too, but are useful regardless
IPv6, from Resolv::IPv6, without the A..z anchors
A method to build a regexp that matches a list of something separated by optional commas and/or the word “and”, an optionally repeated prefix, and whitespace.
# File lib/rbot/core/utils/extends.rb, line 373 def Regexp.new_list(reg, pfx = "") if pfx.kind_of?(String) and pfx.empty? return %r(#{reg}(?:,?(?:\s+and)?\s+#{reg})*) else return %r(#{reg}(?:,?(?:\s+and)?(?:\s+#{pfx})?\s+#{reg})*) end end
a Regexp has captures when its source has open parenthesis which are preceded by an even number of slashes and not followed by a question mark
# File lib/rbot/messagemapper.rb, line 7 def has_captures? self.source.match(/(?:^|[^\])(?:\\)*\([^?]/) end
The MessageMapper cleanup method: does both remove_capture and #remove_head_tail
# File lib/rbot/messagemapper.rb, line 27 def mm_cleanup new = self.source.gsub(/(^|[^\])((?:\\)*)\(([^?])/) { "%s%s(?:%s" % [$1, $2, $3] }.sub(/^\^/,'').sub(/\$$/,'') Regexp.new(new, self.options) end
We may want to remove captures
# File lib/rbot/messagemapper.rb, line 12 def remove_captures new = self.source.gsub(/(^|[^\])((?:\\)*)\(([^?])/) { "%s%s(?:%s" % [$1, $2, $3] } Regexp.new(new, self.options) end
We may want to remove head and tail anchors
# File lib/rbot/messagemapper.rb, line 20 def remove_head_tail new = self.source.sub(/^\^/,'').sub(/\$$/,'') Regexp.new(new, self.options) end