class Regexp
Public Instance Methods
Add regular expressions.
/a/ + /b/ == /(?-mix:a)(?-mix:b)/
Functionally equivalent to:
/ab/
CREDIT: Tyler Rick
# File lib/core/facets/regexp/op_add.rb, line 12 def +(other) other = Regexp.escape(other) if other.is_a?(String) /#{self}#{other}/ end
Returns the number of backreferencing subexpressions.
/(a)(b)(c)/.arity #=> 3 /(a(b(c)))/.arity #=> 3
Note that this is not perfect, especially with regards to x and embedded comments.
CREDIT: Trans
# File lib/core/facets/regexp/arity.rb, line 13 def arity source.scan( /(?!\\)[(](?!\?[#=:!>-imx])/ ).length end
Is a regular expression multiline?
/x/.multiline? #=> false /x/m.multiline? #=> true
# File lib/core/facets/regexp/multiline.rb, line 8 def multiline? options & MULTILINE == MULTILINE end
Useful to pass a Regexp
as a block that will match elements
%w[ joe_the_dog mary_the_cat ].index &/cat/ #=> 1
Returns [Proc] which passes it’s argument to Regexp#match method.
# File lib/core/facets/regexp/to_proc.rb, line 9 def to_proc proc { |x| match(x) } end
Simply returns itself. Helpful when converting strings to regular expressions, where regexp might occur as well –in the same vien as using to_s on symbols. The parameter is actaully a dummy parameter to coincide with String#to_re
.
/abc/.to_re #=> /abc/
CREDIT: Trans
# File lib/core/facets/regexp/to_re.rb, line 24 def to_re(esc=false) self # Of course, things really should know how to say "I" ;) end
Like to_re
, but following Ruby’s formal definitions, only a Regular expression type object will respond to this.
Note that to be of much real use this should be defined in core Ruby.
CREDIT: Florian Gross
# File lib/core/facets/regexp/to_re.rb, line 10 def to_regexp self end
Operator form of ‘Regexp.union`.
/a/ | /b/ #=> /(?-mix:a)|(?-mix:b)/
If other
is not a Regexp
it is passed to Regexp.escape.
# File lib/core/facets/regexp/op_or.rb, line 9 def |(other) other = Regexp.escape(other) unless Regexp === other Regexp.union(self, other) end