Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
Regexp.new
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;
}
With argument string given, returns a new regexp with the given string and options:
r = Regexp.new('foo') # => /foo/ r.source # => "foo" r.options # => 0
Optional argument options is one of the following:
-
A
Stringof options:ExampleRegexp.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, andRegexp::NOENCODING:ExampleRegexp.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
-
nilorfalse, 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.
options = Regexp::MULTILINE r = Regexp.new('foo', options, 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
Ruby version changes
-
3.2
-
Regexp.newnow supports passing the regexp flags not only as anInteger, but also as aString. Unknown flags raiseArgumentError. Otherwise, anything other thantrue,false,nilorIntegerwill be warned. [Feature #18788] -
Regexp.timeout=has been added. Also,Regexp.newnew supports timeout keyword. See [Feature #17837]
-