class Regexp
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
Constants
- DEC_IP_ADDR
- DEC_OCTET
- DIGITS
We start with some general-purpose ones which will be used in the
Irc
module too, but are useful regardless- HEX_16BIT
IPv6, from Resolv::IPv6, without the A..z anchors
- HEX_DIGIT
- HEX_DIGITS
- HEX_IP_ADDR
- HEX_OCTET
- IN_ON
- IP6_6Hex4Dec
- IP6_8Hex
- IP6_ADDR
- IP6_CompressedHex
- IP6_CompressedHex4Dec
- IP_ADDR
Public Class Methods
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 405 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
Public Instance Methods
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