Class: Regexp

Inherits:
Object show all
Defined in:
/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb,
/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/extends.rb,
/home/apoc/projects/ruby/rbot/lib/rbot/irc.rb

Overview

We extend the Regexp class with an Irc module which will contain some Irc-specific regexps

Defined Under Namespace

Modules: Irc

Constant Summary

IN_ON =
/in|on/
DIGITS =

We start with some general-purpose ones which will be used in the Irc module too, but are useful regardless

/\d+/
HEX_DIGIT =
/[0-9A-Fa-f]/
HEX_DIGITS =
/#{HEX_DIGIT}+/
HEX_OCTET =
/#{HEX_DIGIT}#{HEX_DIGIT}?/
DEC_OCTET =
/[01]?\d?\d|2[0-4]\d|25[0-5]/
DEC_IP_ADDR =
/#{DEC_OCTET}\.#{DEC_OCTET}\.#{DEC_OCTET}\.#{DEC_OCTET}/
HEX_IP_ADDR =
/#{HEX_OCTET}\.#{HEX_OCTET}\.#{HEX_OCTET}\.#{HEX_OCTET}/
IP_ADDR =
/#{DEC_IP_ADDR}|#{HEX_IP_ADDR}/
HEX_16BIT =

IPv6, from Resolv::IPv6, without the A..z anchors

/#{HEX_DIGIT}{1,4}/
IP6_8Hex =
/(?:#{HEX_16BIT}:){7}#{HEX_16BIT}/
IP6_CompressedHex =
/((?:#{HEX_16BIT}(?::#{HEX_16BIT})*)?)::((?:#{HEX_16BIT}(?::#{HEX_16BIT})*)?)/
IP6_6Hex4Dec =
/((?:#{HEX_16BIT}:){6,6})#{DEC_IP_ADDR}/
IP6_CompressedHex4Dec =
/((?:#{HEX_16BIT}(?::#{HEX_16BIT})*)?)::((?:#{HEX_16BIT}:)*)#{DEC_IP_ADDR}/
IP6_ADDR =
/(?:#{IP6_8Hex})|(?:#{IP6_CompressedHex})|(?:#{IP6_6Hex4Dec})|(?:#{IP6_CompressedHex4Dec})/

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) new_list(reg, pfx = "")

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.



393
394
395
396
397
398
399
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/extends.rb', line 393

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

Instance Method Details

- (Boolean) has_captures?

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

Returns:

  • (Boolean)


7
8
9
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 7

def has_captures?
  self.source.match(/(?:^|[^\\])(?:\\\\)*\([^?]/)
end

- (Object) mm_cleanup

The MessageMapper cleanup method: does both remove_capture and remove_head_tail



27
28
29
30
31
32
# File '/home/apoc/projects/ruby/rbot/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

- (Object) remove_captures

We may want to remove captures



12
13
14
15
16
17
# File '/home/apoc/projects/ruby/rbot/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

- (Object) remove_head_tail

We may want to remove head and tail anchors



20
21
22
23
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 20

def remove_head_tail
  new = self.source.sub(/^\^/,'').sub(/\$$/,'')
  Regexp.new(new, self.options)
end