Class

Regexp

Inheritance
< Object

Document-class: Regexp

A Regexp holds a regular expression, used to match a pattern against strings. Regexps are created using the /…/ and %r{…} literals, and by the Regexp::new constructor.

Constants

Name   Description
EXTENDED = INT2FIX(RE_OPTION_EXTENDED)
IGNORECASE = INT2FIX(RE_OPTION_IGNORECASE)
MULTILINE = INT2FIX(RE_OPTION_MULTILINE)

Methods

Class

Visibility Signature
public compile (...)
public escape (...)
public last_match (...)
public new (...)
public quote (...)
public union (...)
public yaml_new ( klass, tag, val )

Instance

Visibility Signature
public & (other)
public == (p1)
public === (p1)
public =~ (p1)
public casefold? ()
public eql? (p1)
public hash ()
public inspect ()
public kcode ()
public match (p1)
public options ()
public source ()
public to_s ()
public to_yaml ( opts = {} )
public | (other)
public ~ ()

Class Method Detail

compile(...)

Synonym for Regexp.new

Regexp.escape(str) => a_str
Regexp.quote(str) => a_str

Escapes any characters that would have special meaning in a regular expression. Returns a new escaped string, or self if no characters are escaped. For any string, Regexp.escape(str)=~str will be true.

   Regexp.escape('\\*?{}.')   #=> \\\\\*\?\{\}\.

Regexp.last_match => matchdata
Regexp.last_match(fixnum) => str

The first form returns the MatchData object generated by the last successful pattern match. Equivalent to reading the global variable $~. The second form returns the nth field in this MatchData object.

   /c(.)t/ =~ 'cat'       #=> 0
   Regexp.last_match      #=> #<MatchData:0x401b3d30>
   Regexp.last_match(0)   #=> "cat"
   Regexp.last_match(1)   #=> "a"
   Regexp.last_match(2)   #=> nil

Regexp.new(string [, options [, lang]]) => regexp
Regexp.new(regexp) => regexp
Regexp.compile(string [, options [, lang]]) => regexp
Regexp.compile(regexp) => regexp

Constructs a new regular expression from pattern, which can be either a String or a Regexp (in which case that regexp‘s options are propagated, and new options may not be specified (a change as of Ruby 1.8). If options is a Fixnum, it should be one or more of the constants Regexp::EXTENDED, Regexp::IGNORECASE, and Regexp::MULTILINE, or-ed together. Otherwise, if options is not nil, the regexp will be case insensitive. The lang parameter enables multibyte support for the regexp: `n’, `N’ = none, `e’, `E’ = EUC, `s’, `S’ = SJIS, `u’, `U’ = UTF-8.

   r1 = Regexp.new('^a-z+:\\s+\w+')           #=> /^a-z+:\s+\w+/
   r2 = Regexp.new('cat', true)               #=> /cat/i
   r3 = Regexp.new('dog', Regexp::EXTENDED)   #=> /dog/x
   r4 = Regexp.new(r2)                        #=> /cat/i

Regexp.escape(str) => a_str
Regexp.quote(str) => a_str

Escapes any characters that would have special meaning in a regular expression. Returns a new escaped string, or self if no characters are escaped. For any string, Regexp.escape(str)=~str will be true.

   Regexp.escape('\\*?{}.')   #=> \\\\\*\?\{\}\.

Regexp.union(pat1, pat2, ...) => new_regexp
Regexp.union(pats_ary) => new_regexp

Return a Regexp object that is the union of the given patterns, i.e., will match any of its parts. The patterns can be Regexp objects, in which case their options will be preserved, or Strings. If no patterns are given, returns /(?!)/.

   Regexp.union                         #=> /(?!)/
   Regexp.union("penzance")             #=> /penzance/
   Regexp.union("a+b*c")                #=> /a\+b\*c/
   Regexp.union("skiing", "sledding")   #=> /skiing|sledding/
   Regexp.union(["skiing", "sledding"]) #=> /skiing|sledding/
   Regexp.union(/dogs/, /cats/i)        #=> /(?-mix:dogs)|(?i-mx:cats)/

yaml_new( klass, tag, val )

Instance Method Detail

&(other)

rxp == other_rxp => true or false
rxp.eql?(other_rxp) => true or false

Equality—Two regexps are equal if their patterns are identical, they have the same character set code, and their casefold? values are the same.

   /abc/  == /abc/x   #=> false
   /abc/  == /abc/i   #=> false
   /abc/u == /abc/n   #=> false

rxp === str => true or false

Case Equality—Synonym for Regexp#=~ used in case statements.

   a = "HELLO"
   case a
   when /^[a-z]*$/; print "Lower case\n"
   when /^[A-Z]*$/; print "Upper case\n"
   else;            print "Mixed case\n"
   end

produces:

   Upper case

rxp.match(str) => matchdata or nil

Returns a MatchData object describing the match, or nil if there was no match. This is equivalent to retrieving the value of the special variable $~ following a normal match.

   /(.)(.)(.)/.match("abc")[2]   #=> "b"

rxp.casefold? => true or false

Returns the value of the case-insensitive flag.

rxp == other_rxp => true or false
rxp.eql?(other_rxp) => true or false

Equality—Two regexps are equal if their patterns are identical, they have the same character set code, and their casefold? values are the same.

   /abc/  == /abc/x   #=> false
   /abc/  == /abc/i   #=> false
   /abc/u == /abc/n   #=> false

rxp.hash => fixnum

Produce a hash based on the text and options of this regular expression.

rxp.inspect => string

Produce a nicely formatted string-version of rxp. Perhaps surprisingly, inspect actually produces the more natural version of the string than to_s.

    /ab+c/ix.to_s         #=> /ab+c/ix

rxp.kcode => str

Returns the character set code for the regexp.

rxp.match(str) => matchdata or nil

Returns a MatchData object describing the match, or nil if there was no match. This is equivalent to retrieving the value of the special variable $~ following a normal match.

   /(.)(.)(.)/.match("abc")[2]   #=> "b"

rxp.options => fixnum

Returns the set of bits corresponding to the options used when creating this Regexp (see Regexp::new for details. Note that additional bits may be set in the returned options: these are used internally by the regular expression code. These extra bits are ignored if the options are passed to Regexp::new.

   Regexp::IGNORECASE                  #=> 1
   Regexp::EXTENDED                    #=> 2
   Regexp::MULTILINE                   #=> 4

   /cat/.options                       #=> 128
   /cat/ix.options                     #=> 131
   Regexp.new('cat', true).options     #=> 129
   Regexp.new('cat', 0, 's').options   #=> 384

   r = /cat/ix
   Regexp.new(r.source, r.options)     #=> /cat/ix

rxp.source => str

Returns the original string of the pattern.

   /ab+c/ix.source   #=> "ab+c"

rxp.to_s => str

Returns a string containing the regular expression and its options (using the (?xxx:yyy) notation. This string can be fed back in to Regexp::new to a regular expression with the same semantics as the original. (However, Regexp#== may not return true when comparing the two, as the source of the regular expression itself may differ, as the example shows). Regexp#inspect produces a generally more readable version of rxp.

   r1 = /ab+c/ix         #=> /ab+c/ix
   s1 = r1.to_s          #=> "(?ix-m:ab+c)"
   r2 = Regexp.new(s1)   #=> /(?ix-m:ab+c)/
   r1 == r2              #=> false
   r1.source             #=> "ab+c"
   r2.source             #=> "(?ix-m:ab+c)"

to_yaml( opts = {} )

|(other)

~ rxp => integer or nil

Match—Matches rxp against the contents of $_. Equivalent to rxp =~ $_.

   $_ = "input data"
   ~ /at/   #=> 7