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.

Methods
#
C
E
H
I
K
L
M
N
O
Q
S
T
U
Y
#
Constants
IGNORECASE = INT2FIX(RE_OPTION_IGNORECASE)
EXTENDED = INT2FIX(RE_OPTION_EXTENDED)
MULTILINE = INT2FIX(RE_OPTION_MULTILINE)
Class Public methods
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('\\*?{}.')   #=> \\\\\*\?\{\}\.
static VALUE
rb_reg_s_quote(argc, argv)
    int argc;
    VALUE *argv;
{
    VALUE str, kcode;
    int kcode_saved = reg_kcode;

    rb_scan_args(argc, argv, "11", &str, &kcode);
    if (!NIL_P(kcode)) {
        rb_set_kcode(StringValuePtr(kcode));
        curr_kcode = reg_kcode;
        reg_kcode = kcode_saved;
    }
    StringValue(str);
    str = rb_reg_quote(str);
    rb_kcode_reset_option();
    return str;
}
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
static VALUE
rb_reg_s_last_match(argc, argv)
    int argc;
    VALUE *argv;
{
    VALUE nth;

    if (rb_scan_args(argc, argv, "01", &nth) == 1) {
        return rb_reg_nth_match(NUM2INT(nth), rb_backref_get());
    }
    return match_getter();
}
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
static VALUE
rb_reg_initialize_m(argc, argv, self)
    int argc;
    VALUE *argv;
    VALUE self;
{
    const char *s;
    long len;
    int flags = 0;

    if (argc == 0 || argc > 3) {
        rb_raise(rb_eArgError, "wrong number of arguments");
    }
    if (TYPE(argv[0]) == T_REGEXP) {
        if (argc > 1) {
            rb_warn("flags%s ignored", (argc == 3) ? " and encoding": "");
        }
        rb_reg_check(argv[0]);
        flags = RREGEXP(argv[0])->ptr->options & 0xf;
        if (FL_TEST(argv[0], KCODE_FIXED)) {
            switch (RBASIC(argv[0])->flags & KCODE_MASK) {
              case KCODE_NONE:
                flags |= 16;
                break;
              case KCODE_EUC:
                flags |= 32;
                break;
              case KCODE_SJIS:
                flags |= 48;
                break;
              case KCODE_UTF8:
                flags |= 64;
                break;
              default:
                break;
            }
        }
        s = RREGEXP(argv[0])->str;
        len = RREGEXP(argv[0])->len;
    }
    else {
        if (argc >= 2) {
            if (FIXNUM_P(argv[1])) flags = FIX2INT(argv[1]);
            else if (RTEST(argv[1])) flags = RE_OPTION_IGNORECASE;
        }
        if (argc == 3 && !NIL_P(argv[2])) {
            char *kcode = StringValuePtr(argv[2]);

            flags &= ~0x70;
            switch (kcode[0]) {
              case 'n': case 'N':
                flags |= 16;
                break;
              case 'e': case 'E':
                flags |= 32;
                break;
              case 's': case 'S':
                flags |= 48;
                break;
              case 'u': case 'U':
                flags |= 64;
                break;
              default:
                break;
            }
        }
        s = StringValuePtr(argv[0]);
        len = RSTRING(argv[0])->len;
    }
    rb_reg_initialize(self, s, len, flags);
    return self;
}
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('\\*?{}.')   #=> \\\\\*\?\{\}\.
static VALUE
rb_reg_s_quote(argc, argv)
    int argc;
    VALUE *argv;
{
    VALUE str, kcode;
    int kcode_saved = reg_kcode;

    rb_scan_args(argc, argv, "11", &str, &kcode);
    if (!NIL_P(kcode)) {
        rb_set_kcode(StringValuePtr(kcode));
        curr_kcode = reg_kcode;
        reg_kcode = kcode_saved;
    }
    StringValue(str);
    str = rb_reg_quote(str);
    rb_kcode_reset_option();
    return str;
}
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)/
static VALUE
rb_reg_s_union_m(VALUE self, VALUE args)
{
    VALUE v;
    if (RARRAY_LEN(args) == 1 &&
        !NIL_P(v = rb_check_array_type(rb_ary_entry(args, 0)))) {
        return rb_reg_s_union(self, v);
    }
    return rb_reg_s_union(self, args);
}
yaml_new( klass, tag, val )
     # File lib/yaml/rubytypes.rb, line 255
255:     def Regexp.yaml_new( klass, tag, val )
256:         if String === val and val =~ /^\/(.*)\/([mix]*)$/
257:             val = { 'regexp' => $1, 'mods' => $2 }
258:         end
259:         if Hash === val
260:             mods = nil
261:             unless val['mods'].to_s.empty?
262:                 mods = 0x00
263:                 mods |= Regexp::EXTENDED if val['mods'].include?( 'x' )
264:                 mods |= Regexp::IGNORECASE if val['mods'].include?( 'i' )
265:                 mods |= Regexp::MULTILINE if val['mods'].include?( 'm' )
266:             end
267:             val.delete( 'mods' )
268:             r = YAML::object_maker( klass, {} )
269:             Regexp.instance_method(:initialize).
270:                   bind(r).
271:                   call( val.delete( 'regexp' ), mods )
272:             val.each { |k,v| r.instance_variable_set( k, v ) }
273:             r
274:         else
275:             raise YAML::TypeError, "Invalid Regular expression: " + val.inspect
276:         end
277:     end
Instance Public methods
&(other)
    # File lib/eregex.rb, line 29
29:   def &(other)
30:     RegAnd.new(self, other)
31:   end
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
static VALUE
rb_reg_equal(re1, re2)
    VALUE re1, re2;
{
    if (re1 == re2) return Qtrue;
    if (TYPE(re2) != T_REGEXP) return Qfalse;
    rb_reg_check(re1); rb_reg_check(re2);
    if (RREGEXP(re1)->len != RREGEXP(re2)->len) return Qfalse;
    if (memcmp(RREGEXP(re1)->str, RREGEXP(re2)->str, RREGEXP(re1)->len) == 0 &&
        rb_reg_cur_kcode(re1) == rb_reg_cur_kcode(re2) &&
        RREGEXP(re1)->ptr->options == RREGEXP(re2)->ptr->options) {
        return Qtrue;
    }
    return Qfalse;
}
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
VALUE
rb_reg_eqq(re, str)
    VALUE re, str;
{
    long start;

    if (TYPE(str) != T_STRING) {
        str = rb_check_string_type(str);
        if (NIL_P(str)) {
            rb_backref_set(Qnil);
            return Qfalse;
        }
    }
    StringValue(str);
    start = rb_reg_search(re, str, 0, 0);
    if (start < 0) {
        return Qfalse;
    }
    return Qtrue;
}
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"
VALUE
rb_reg_match(re, str)
    VALUE re, str;
{
    long start;

    if (NIL_P(str)) {
        rb_backref_set(Qnil);
        return Qnil;
    }
    StringValue(str);
    start = rb_reg_search(re, str, 0, 0);
    if (start < 0) {
        return Qnil;
    }
    return LONG2FIX(start);
}
rxp.casefold? => true or false

Returns the value of the case-insensitive flag.

static VALUE
rb_reg_casefold_p(re)
    VALUE re;
{
    rb_reg_check(re);
    if (RREGEXP(re)->ptr->options & RE_OPTION_IGNORECASE) return Qtrue;
    return Qfalse;
}
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
static VALUE
rb_reg_equal(re1, re2)
    VALUE re1, re2;
{
    if (re1 == re2) return Qtrue;
    if (TYPE(re2) != T_REGEXP) return Qfalse;
    rb_reg_check(re1); rb_reg_check(re2);
    if (RREGEXP(re1)->len != RREGEXP(re2)->len) return Qfalse;
    if (memcmp(RREGEXP(re1)->str, RREGEXP(re2)->str, RREGEXP(re1)->len) == 0 &&
        rb_reg_cur_kcode(re1) == rb_reg_cur_kcode(re2) &&
        RREGEXP(re1)->ptr->options == RREGEXP(re2)->ptr->options) {
        return Qtrue;
    }
    return Qfalse;
}
rxp.hash => fixnum

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

static VALUE
rb_reg_hash(re)
    VALUE re;
{
    int hashval, len;
    char *p;

    rb_reg_check(re);
    hashval = RREGEXP(re)->ptr->options;
    len = RREGEXP(re)->len;
    p  = RREGEXP(re)->str;
    while (len--) {
        hashval = hashval * 33 + *p++;
    }
    hashval = hashval + (hashval>>5);
    
    return INT2FIX(hashval);
}
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
static VALUE
rb_reg_inspect(re)
    VALUE re;
{
    rb_reg_check(re);
    return rb_reg_desc(RREGEXP(re)->str, RREGEXP(re)->len, re);
}
rxp.kcode => str

Returns the character set code for the regexp.

static VALUE
rb_reg_kcode_m(re)
    VALUE re;
{
    const char *kcode;

    if (FL_TEST(re, KCODE_FIXED)) {
        switch (RBASIC(re)->flags & KCODE_MASK) {
          case KCODE_NONE:
            kcode = "none"; break;
          case KCODE_EUC:
            kcode = "euc"; break;
          case KCODE_SJIS:
            kcode = "sjis"; break;
          case KCODE_UTF8:
            kcode = "utf8"; break;
          default:
            rb_bug("unknown kcode - should not happen");
            break;
        }
        return rb_str_new2(kcode);
    }
    return Qnil;
}
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"
static VALUE
rb_reg_match_m(re, str)
    VALUE re, str;
{
    VALUE result = rb_reg_match(re, str);

    if (NIL_P(result)) return Qnil;
    result = rb_backref_get();
    rb_match_busy(result);
    return result;
}
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
static VALUE
rb_reg_options_m(re)
    VALUE re;
{
    int options = rb_reg_options(re);
    return INT2NUM(options);
}
rxp.source => str

Returns the original string of the pattern.

   /ab+c/ix.source   #=> "ab+c"
static VALUE
rb_reg_source(re)
    VALUE re;
{
    VALUE str;

    rb_reg_check(re);
    str = rb_str_new(RREGEXP(re)->str,RREGEXP(re)->len);
    if (OBJ_TAINTED(re)) OBJ_TAINT(str);
    return str;
}
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)"
static VALUE
rb_reg_to_s(re)
    VALUE re;
{
    int options;
    const int embeddable = RE_OPTION_MULTILINE|RE_OPTION_IGNORECASE|RE_OPTION_EXTENDED;
    long len;
    const char* ptr;
    VALUE str = rb_str_buf_new2("(?");

    rb_reg_check(re);

    options = RREGEXP(re)->ptr->options;
    ptr = RREGEXP(re)->str;
    len = RREGEXP(re)->len;
  again:
    if (len >= 4 && ptr[0] == '(' && ptr[1] == '?') {
        int err = 1;
        ptr += 2;
        if ((len -= 2) > 0) {
            do {
                if (*ptr == 'm') {
                    options |= RE_OPTION_MULTILINE;
                }
                else if (*ptr == 'i') {
                    options |= RE_OPTION_IGNORECASE;
                }
                else if (*ptr == 'x') {
                    options |= RE_OPTION_EXTENDED;
                }
                else break;
                ++ptr;
            } while (--len > 0);
        }
        if (len > 1 && *ptr == '-') {
            ++ptr;
            --len;
            do {
                if (*ptr == 'm') {
                    options &= ~RE_OPTION_MULTILINE;
                }
                else if (*ptr == 'i') {
                    options &= ~RE_OPTION_IGNORECASE;
                }
                else if (*ptr == 'x') {
                    options &= ~RE_OPTION_EXTENDED;
                }
                else break;
                ++ptr;
            } while (--len > 0);
        }
        if (*ptr == ')') {
            --len;
            ++ptr;
            goto again;
        }
        if (*ptr == ':' && ptr[len-1] == ')') {
            Regexp *rp;
            rb_kcode_set_option(re);
            rp = ALLOC(Regexp);
            MEMZERO((char *)rp, Regexp, 1);
            err = re_compile_pattern(++ptr, len -= 2, rp) != 0;
            rb_kcode_reset_option();
            re_free_pattern(rp);
        }
        if (err) {
            options = RREGEXP(re)->ptr->options;
            ptr = RREGEXP(re)->str;
            len = RREGEXP(re)->len;
        }
    }

    if (options & RE_OPTION_MULTILINE) rb_str_buf_cat2(str, "m");
    if (options & RE_OPTION_IGNORECASE) rb_str_buf_cat2(str, "i");
    if (options & RE_OPTION_EXTENDED) rb_str_buf_cat2(str, "x");

    if ((options & embeddable) != embeddable) {
        rb_str_buf_cat2(str, "-");
        if (!(options & RE_OPTION_MULTILINE)) rb_str_buf_cat2(str, "m");
        if (!(options & RE_OPTION_IGNORECASE)) rb_str_buf_cat2(str, "i");
        if (!(options & RE_OPTION_EXTENDED)) rb_str_buf_cat2(str, "x");
    }

    rb_str_buf_cat2(str, ":");
    rb_reg_expr_str(str, ptr, len);
    rb_str_buf_cat2(str, ")");

    OBJ_INFECT(str, re);
    return str;
}
to_yaml( opts = {} )
     # File lib/yaml/rubytypes.rb, line 278
278:         def to_yaml( opts = {} )
279:                 YAML::quick_emit( nil, opts ) do |out|
280:             if to_yaml_properties.empty?
281:                 out.scalar( taguri, self.inspect, :plain )
282:             else
283:                 out.map( taguri, to_yaml_style ) do |map|
284:                     src = self.inspect
285:                     if src =~ /\A\/(.*)\/([a-z]*)\Z/
286:                         map.add( 'regexp', $1 )
287:                         map.add( 'mods', $2 )
288:                     else
289:                                 raise YAML::TypeError, "Invalid Regular expression: " + src
290:                     end
291:                     to_yaml_properties.each do |m|
292:                         map.add( m, instance_variable_get( m ) )
293:                     end
294:                 end
295:             end
296:         end
297:         end
|(other)
    # File lib/eregex.rb, line 26
26:   def |(other)
27:     RegOr.new(self, other)
28:   end
~ rxp => integer or nil

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

   $_ = "input data"
   ~ /at/   #=> 7
VALUE
rb_reg_match2(re)
    VALUE re;
{
    long start;
    VALUE line = rb_lastline_get();

    if (TYPE(line) != T_STRING) {
        rb_backref_set(Qnil);
        return Qnil;
    }

    start = rb_reg_search(re, line, 0, 0);
    if (start < 0) {
        return Qnil;
    }
    return LONG2FIX(start);
}