Module: Whois::ExtendedRegexp::InstanceMethods
- Defined in:
- lib/support/extended_regexp.rb
Overview
Extensions for Ruby’s core Regexp class.
Instance Method Summary collapse
-
#interpolate(*args) ⇒ Object
Find and replace literal values in a regular expression source string.
-
#invert! ⇒ Object
Inverted Regular Expressions.
-
#inverted? ⇒ Boolean
Is this an inverted regular expression?.
-
#match_with_inversion(*args, &block) ⇒ Object
Run an inversion-aware match using this Regexp.
-
#new_options(option_string) ⇒ Object
Make a copy of the current regular expression object but with new options.
- #set_inverted(value = true) ⇒ Object
-
#uninvert! ⇒ Object
Uninvert this regular expression.
Instance Method Details
#interpolate(*args) ⇒ Object
Find and replace literal values in a regular expression source string. You pass it a Hash where the keys are either strings or regular expressions to search for, and their values are the replacement values. A completely new Regexp object is returned and options are preserved.
/a __REPLACE___ day/i.interpolate("__REPLACE__" => "fun") => /a fun day/i
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/support/extended_regexp.rb', line 18 def interpolate(*args) if args.length > 1 replacements = Hash[*args] elsif args.first.kind_of?(Hash) replacements = args.first else raise ArgumentError, "Must pass hash to interpolate." end string = self.source replacements.each do |key, value| if key.kind_of?(Regexp) string.gsub!(key, value.to_s) else string.gsub!(/#{self.class.escape(key)}/im, value.to_s) end end string = (self.source.to_s).interpolate(*args) self.class.new(string, self.) end |
#invert! ⇒ Object
Inverted Regular Expressions
Gives the ability to invert a regular expression so that running a match against it will yield a true result when it does NOT match the target string.
Invert this regular expression.
63 64 65 66 |
# File 'lib/support/extended_regexp.rb', line 63 def invert! @inverted = true self end |
#inverted? ⇒ Boolean
Is this an inverted regular expression?
79 80 81 |
# File 'lib/support/extended_regexp.rb', line 79 def inverted? @inverted rescue false end |
#match_with_inversion(*args, &block) ⇒ Object
Run an inversion-aware match using this Regexp.
84 85 86 87 88 89 90 91 |
# File 'lib/support/extended_regexp.rb', line 84 def match_with_inversion(*args, &block) result = self.match(*args, &block) if @inverted result.nil? ? true : false else result end end |
#new_options(option_string) ⇒ Object
Make a copy of the current regular expression object but with new options. Options are given as a string, like so:
/testing/i.('im') => /testing/im
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/support/extended_regexp.rb', line 45 def (option_string) = [] { :i => Regexp::IGNORECASE, :x => Regexp::EXTENDED, :m => Regexp::MULTILINE }.each_pair do |charcode, constant| .push(constant) if option_string =~ /#{charcode.to_s}/i end self.class.new(self.source, ) end |
#set_inverted(value = true) ⇒ Object
74 75 76 |
# File 'lib/support/extended_regexp.rb', line 74 def set_inverted(value = true) @inverted = (value) ? true : false end |
#uninvert! ⇒ Object
Uninvert this regular expression.
69 70 71 72 |
# File 'lib/support/extended_regexp.rb', line 69 def uninvert! @inverted = false self end |