Class: Regexp

Inherits:
Object show all
Defined in:
re.c,
re.c

Overview

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.

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(string[, options [, lang]]) ⇒ Regexp #new(regexp) ⇒ Regexp #compile(string[, options [, lang]]) ⇒ 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

Overloads:

  • #new(string[, options [, lang]]) ⇒ Regexp
  • #new(regexp) ⇒ Regexp
  • #compile(string[, options [, lang]]) ⇒ Regexp
  • #compile(regexp) ⇒ Regexp


1654
1655
1656
# File 're.c', line 1654

static VALUE
rb_reg_initialize_m(argc, argv, self)
int argc;

Class Method Details

.compileObject

Synonym for Regexp.new

.escape(str) ⇒ String .quote(str) ⇒ String

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('\\*?{}.')   #=> \\\\\*\?\{\}\.

Overloads:



1826
1827
1828
# File 're.c', line 1826

static VALUE
rb_reg_s_quote(argc, argv)
int argc;

.last_matchMatchData .last_match(fixnum) ⇒ String

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

Overloads:



2204
2205
2206
# File 're.c', line 2204

static VALUE
rb_reg_s_last_match(argc, argv)
int argc;

.escape(str) ⇒ String .quote(str) ⇒ String

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('\\*?{}.')   #=> \\\\\*\?\{\}\.

Overloads:



1826
1827
1828
# File 're.c', line 1826

static VALUE
rb_reg_s_quote(argc, argv)
int argc;

.union([pattern]) ⇒ String

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 arguments are given, returns /(?!)/.

Regexp.union                         #=> /(?!)/
Regexp.union("penzance")             #=> /penzance/
Regexp.union("skiing", "sledding")   #=> /skiing|sledding/
Regexp.union(/dogs/, /cats/i)        #=> /(?-mix:dogs)|(?i-mx:cats)/

Returns:



1910
1911
1912
# File 're.c', line 1910

static VALUE
rb_reg_s_union(argc, argv)
int argc;

Instance Method Details

#==(other_rxp) ⇒ Boolean #eql?(other_rxp) ⇒ Boolean

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

Overloads:

  • #==(other_rxp) ⇒ Boolean

    Returns:

    • (Boolean)
  • #eql?(other_rxp) ⇒ Boolean

    Returns:

    • (Boolean)


1483
1484
1485
# File 're.c', line 1483

static VALUE
rb_reg_equal(re1, re2)
VALUE re1, re2;

#===(str) ⇒ Boolean

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

Returns:

  • (Boolean)


1548
1549
1550
# File 're.c', line 1548

VALUE
rb_reg_eqq(re, str)
VALUE re, str;

#match(str) ⇒ MatchData?

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"

Returns:



1511
1512
1513
# File 're.c', line 1511

VALUE
rb_reg_match(re, str)
VALUE re, str;

#casefold?Boolean

Returns the value of the case-insensitive flag.

Returns:

  • (Boolean)


545
546
547
# File 're.c', line 545

static VALUE
rb_reg_casefold_p(re)
VALUE re;

#==(other_rxp) ⇒ Boolean #eql?(other_rxp) ⇒ Boolean

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

Overloads:

  • #==(other_rxp) ⇒ Boolean

    Returns:

    • (Boolean)
  • #eql?(other_rxp) ⇒ Boolean

    Returns:

    • (Boolean)


1483
1484
1485
# File 're.c', line 1483

static VALUE
rb_reg_equal(re1, re2)
VALUE re1, re2;

#hashFixnum

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

Returns:



1449
1450
1451
# File 're.c', line 1449

static VALUE
rb_reg_hash(re)
VALUE re;

#initialize_copyObject

:nodoc:



1987
1988
1989
# File 're.c', line 1987

static VALUE
rb_reg_init_copy(copy, re)
VALUE copy, re;

#inspectString

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

Returns:



402
403
404
# File 're.c', line 402

static VALUE
rb_reg_inspect(re)
VALUE re;

#kcodeString

Returns the character set code for the regexp.

Returns:



594
595
596
# File 're.c', line 594

static VALUE
rb_reg_kcode_m(re)
VALUE re;

#match(str) ⇒ MatchData?

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"

Returns:



1612
1613
1614
# File 're.c', line 1612

static VALUE
rb_reg_match_m(re, str)
VALUE re, str;

#optionsFixnum

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

Returns:



578
579
580
# File 're.c', line 578

static VALUE
rb_reg_options_m(re)
VALUE re;

#sourceString

Returns the original string of the pattern.

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

Returns:



379
380
381
# File 're.c', line 379

static VALUE
rb_reg_source(re)
VALUE re;

#to_sString

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)"

Returns:



431
432
433
# File 're.c', line 431

static VALUE
rb_reg_to_s(re)
VALUE re;

#~(rxp) ⇒ Integer?

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

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

Returns:



1581
1582
1583
# File 're.c', line 1581

VALUE
rb_reg_match2(re)
VALUE re;