Method: Regexp#initialize
- Defined in:
- re.c
#new(string, options = 0, timeout: nil) ⇒ Regexp #new(regexp, timeout: nil) ⇒ Regexp
With argument string
given, returns a new regexp with the given string and options:
r = Regexp.new('foo') # => /foo/
r.source # => "foo"
r. # => 0
Optional argument options
is one of the following:
-
A String of options:
Regexp.new('foo', 'i') # => /foo/i Regexp.new('foo', 'im') # => /foo/im
-
The bit-wise OR of one or more of the constants Regexp::EXTENDED, Regexp::IGNORECASE, Regexp::MULTILINE, and Regexp::NOENCODING:
Regexp.new('foo', Regexp::IGNORECASE) # => /foo/i Regexp.new('foo', Regexp::EXTENDED) # => /foo/x Regexp.new('foo', Regexp::MULTILINE) # => /foo/m Regexp.new('foo', Regexp::NOENCODING) # => /foo/n flags = Regexp::IGNORECASE | Regexp::EXTENDED | Regexp::MULTILINE Regexp.new('foo', flags) # => /foo/mix
-
nil
orfalse
, which is ignored. -
Any other truthy value, in which case the regexp will be case-insensitive.
If optional keyword argument timeout
is given, its float value overrides the timeout interval for the class, Regexp.timeout. If nil
is passed as +timeout, it uses the timeout interval for the class, Regexp.timeout.
With argument regexp
given, returns a new regexp. The source, options, timeout are the same as regexp
. options
and n_flag
arguments are ineffective. The timeout can be overridden by timeout
keyword.
= Regexp::MULTILINE
r = Regexp.new('foo', , timeout: 1.1) # => /foo/m
r2 = Regexp.new(r) # => /foo/m
r2.timeout # => 1.1
r3 = Regexp.new(r, timeout: 3.14) # => /foo/m
r3.timeout # => 3.14
4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 |
# File 're.c', line 4004
static VALUE
rb_reg_initialize_m(int argc, VALUE *argv, VALUE self)
{
struct reg_init_args args;
VALUE re = reg_extract_args(argc, argv, &args);
if (NIL_P(re)) {
reg_init_args(self, args.str, args.enc, args.flags);
}
else {
reg_copy(self, re);
}
set_timeout(&RREGEXP_PTR(self)->timelimit, args.timeout);
return self;
}
|