Class: Regexp
Overview
:include: doc/_regexp.rdoc
Defined Under Namespace
Classes: TimeoutError
Constant Summary collapse
- IGNORECASE =
see Regexp.options and Regexp.new
INT2FIX(ONIG_OPTION_IGNORECASE)
- EXTENDED =
see Regexp.options and Regexp.new
INT2FIX(ONIG_OPTION_EXTEND)
- MULTILINE =
see Regexp.options and Regexp.new
INT2FIX(ONIG_OPTION_MULTILINE)
- FIXEDENCODING =
see Regexp.options and Regexp.new
INT2FIX(ARG_ENCODING_FIXED)
- NOENCODING =
see Regexp.options and Regexp.new
INT2FIX(ARG_ENCODING_NONE)
Class Method Summary collapse
-
.compile ⇒ Object
Alias for Regexp.new.
-
.escape(string) ⇒ Object
Returns a new string that escapes any characters that have special meaning in a regular expression:.
-
.last_match(*args) ⇒ Object
With no argument, returns the value of
$!
, which is the result of the most recent pattern match (see Regexp global variables):. -
.linear_time?(*args) ⇒ Boolean
Returns
true
if matching againstre
can be done in linear time to the input string. -
.escape(string) ⇒ Object
Returns a new string that escapes any characters that have special meaning in a regular expression:.
-
.timeout ⇒ Float?
It returns the current default timeout interval for Regexp matching in second.
-
.timeout=(float) ⇒ Object
It sets the default timeout interval for Regexp matching in second.
-
.try_convert(object) ⇒ Regexp?
Returns
object
if it is a regexp:. -
.union(args) ⇒ Object
Returns a new regexp that is the union of the given patterns:.
Instance Method Summary collapse
-
#==(object) ⇒ Boolean
Returns
true
ifobject
is another Regexp whose pattern, flags, and encoding are the same asself
,false
otherwise:. -
#===(string) ⇒ Boolean
Returns
true
ifself
finds a match instring
:. -
#=~(string) ⇒ Integer?
Returns the integer index (in characters) of the first match for
self
andstring
, ornil
if none; also sets the rdoc-ref:Regexp global variables:. -
#casefold? ⇒ Boolean
Returns
true
if the case-insensitivity flag inself
is set,false
otherwise:. -
#encoding ⇒ Encoding
Returns the Encoding object that represents the encoding of obj.
-
#==(object) ⇒ Boolean
Returns
true
ifobject
is another Regexp whose pattern, flags, and encoding are the same asself
,false
otherwise:. -
#fixed_encoding? ⇒ Boolean
Returns
false
ifself
is applicable to a string with any ASCII-compatible encoding; otherwise returnstrue
:. -
#hash ⇒ Integer
Returns the integer hash value for
self
. -
#initialize(*args) ⇒ Object
constructor
With argument
string
given, returns a new regexp with the given string and options:. -
#initialize_copy(re) ⇒ Object
:nodoc:.
-
#inspect ⇒ String
Returns a nicely-formatted string representation of
self
:. -
#match(*args) ⇒ Object
With no block given, returns the MatchData object that describes the match, if any, or
nil
if none; the search begins at the given characteroffset
instring
:. -
#match?(*args) ⇒ Object
Returns
true
orfalse
to indicate whether the regexp is matched or not without updating $~ and other related variables. -
#named_captures ⇒ Hash
Returns a hash representing named captures of
self
(see Named Captures):. -
#names ⇒ Object
Returns an array of names of captures (see Named Captures):.
-
#options ⇒ Integer
Returns an integer whose bits show the options set in
self
. -
#source ⇒ String
Returns the original string of
self
:. -
#timeout ⇒ Float?
It returns the timeout interval for Regexp matching in second.
-
#to_s ⇒ String
Returns a string showing the options and string of
self
:. -
#~(rxp) ⇒ Integer?
Equivalent to
rxp =~ $_
:.
Constructor Details
#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
3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 |
# File 're.c', line 3956
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;
}
|
Class Method Details
.compile ⇒ Object
Alias for Regexp.new
.escape(string) ⇒ Object
Returns a new string that escapes any characters that have special meaning in a regular expression:
s = Regexp.escape('\*?{}.') # => "\\\\\\*\\?\\{\\}\\."
For any string s
, this call returns a MatchData object:
r = Regexp.new(Regexp.escape(s)) # => /\\\\\\\*\\\?\\\{\\\}\\\./
r.match(s) # => #<MatchData "\\\\\\*\\?\\{\\}\\.">
4143 4144 4145 4146 4147 |
# File 're.c', line 4143
static VALUE
rb_reg_s_quote(VALUE c, VALUE str)
{
return rb_reg_quote(reg_operand(str, TRUE));
}
|
.last_match ⇒ MatchData? .last_match(n) ⇒ String? .last_match(name) ⇒ String?
With no argument, returns the value of $!
, which is the result of the most recent pattern match (see Regexp global variables):
/c(.)t/ =~ 'cat' # => 0
Regexp.last_match # => #<MatchData "cat" 1:"a">
/a/ =~ 'foo' # => nil
Regexp.last_match # => nil
With non-negative integer argument n
, returns the _n_th field in the matchdata, if any, or nil if none:
/c(.)t/ =~ 'cat' # => 0
Regexp.last_match(0) # => "cat"
Regexp.last_match(1) # => "a"
Regexp.last_match(2) # => nil
With negative integer argument n
, counts backwards from the last field:
Regexp.last_match(-1) # => "a"
With string or symbol argument name
, returns the string value for the named capture, if any:
/(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ 'var = val'
Regexp.last_match # => #<MatchData "var = val" lhs:"var"rhs:"val">
Regexp.last_match(:lhs) # => "var"
Regexp.last_match('rhs') # => "val"
Regexp.last_match('foo') # Raises IndexError.
4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 |
# File 're.c', line 4594
static VALUE
rb_reg_s_last_match(int argc, VALUE *argv, VALUE _)
{
if (rb_check_arity(argc, 0, 1) == 1) {
VALUE match = rb_backref_get();
int n;
if (NIL_P(match)) return Qnil;
n = match_backref_number(match, argv[0]);
return rb_reg_nth_match(n, match);
}
return match_getter();
}
|
.linear_time?(re) ⇒ Boolean .linear_time?(string, options = 0) ⇒ Boolean
Returns true
if matching against re
can be done in linear time to the input string.
Regexp.linear_time?(/re/) # => true
Note that this is a property of the ruby interpreter, not of the argument regular expression. Identical regexp can or cannot run in linear time depending on your ruby binary. Neither forward nor backward compatibility is guaranteed about the return value of this method. Our current algorithm is (*1) but this is subject to change in the future. Alternative implementations can also behave differently. They might always return false for everything.
4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 |
# File 're.c', line 4371
static VALUE
rb_reg_s_linear_time_p(int argc, VALUE *argv, VALUE self)
{
struct reg_init_args args;
VALUE re = reg_extract_args(argc, argv, &args);
if (NIL_P(re)) {
re = reg_init_args(rb_reg_alloc(), args.str, args.enc, args.flags);
}
return RBOOL(onig_check_linear_time(RREGEXP_PTR(re)));
}
|
.escape(string) ⇒ Object
Returns a new string that escapes any characters that have special meaning in a regular expression:
s = Regexp.escape('\*?{}.') # => "\\\\\\*\\?\\{\\}\\."
For any string s
, this call returns a MatchData object:
r = Regexp.new(Regexp.escape(s)) # => /\\\\\\\*\\\?\\\{\\\}\\\./
r.match(s) # => #<MatchData "\\\\\\*\\?\\{\\}\\.">
4143 4144 4145 4146 4147 |
# File 're.c', line 4143
static VALUE
rb_reg_s_quote(VALUE c, VALUE str)
{
return rb_reg_quote(reg_operand(str, TRUE));
}
|
.timeout ⇒ Float?
It returns the current default timeout interval for Regexp matching in second. nil
means no default timeout configuration.
4656 4657 4658 4659 4660 4661 4662 |
# File 're.c', line 4656
static VALUE
rb_reg_s_timeout_get(VALUE dummy)
{
double d = hrtime2double(rb_reg_match_time_limit);
if (d == 0.0) return Qnil;
return DBL2NUM(d);
}
|
.timeout=(float) ⇒ Object
It sets the default timeout interval for Regexp matching in second. nil
means no default timeout configuration. This configuration is process-global. If you want to set timeout for each Regexp, use timeout
keyword for Regexp.new
.
Regexp.timeout = 1
/^a*b?a*$/ =~ "a" * 100000 + "x" #=> regexp match timeout (RuntimeError)
4677 4678 4679 4680 4681 4682 4683 4684 4685 |
# File 're.c', line 4677
static VALUE
rb_reg_s_timeout_set(VALUE dummy, VALUE timeout)
{
rb_ractor_ensure_main_ractor("can not access Regexp.timeout from non-main Ractors");
set_timeout(&rb_reg_match_time_limit, timeout);
return timeout;
}
|
.try_convert(object) ⇒ Regexp?
Returns object
if it is a regexp:
Regexp.try_convert(/re/) # => /re/
Otherwise if object
responds to :to_regexp
, calls object.to_regexp
and returns the result.
Returns nil
if object
does not respond to :to_regexp
.
Regexp.try_convert('re') # => nil
Raises an exception unless object.to_regexp
returns a regexp.
4185 4186 4187 4188 4189 |
# File 're.c', line 4185
static VALUE
rb_reg_s_try_convert(VALUE dummy, VALUE re)
{
return rb_check_regexp_type(re);
}
|
.union(*patterns) ⇒ Regexp .union(array_of_patterns) ⇒ Regexp
Returns a new regexp that is the union of the given patterns:
r = Regexp.union(%w[cat dog]) # => /cat|dog/
r.match('cat') # => #<MatchData "cat">
r.match('dog') # => #<MatchData "dog">
r.match('cog') # => nil
For each pattern that is a string, Regexp.new(pattern)
is used:
Regexp.union('penzance') # => /penzance/
Regexp.union('a+b*c') # => /a\+b\*c/
Regexp.union('skiing', 'sledding') # => /skiing|sledding/
Regexp.union(['skiing', 'sledding']) # => /skiing|sledding/
For each pattern that is a regexp, it is used as is, including its flags:
Regexp.union(/foo/i, /bar/m, /baz/x)
# => /(?i-mx:foo)|(?m-ix:bar)|(?x-mi:baz)/
Regexp.union([/foo/i, /bar/m, /baz/x])
# => /(?i-mx:foo)|(?m-ix:bar)|(?x-mi:baz)/
With no arguments, returns /(?!)/
:
Regexp.union # => /(?!)/
If any regexp pattern contains captures, the behavior is unspecified.
4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 |
# File 're.c', line 4339
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);
}
|
Instance Method Details
#==(object) ⇒ Boolean
3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 |
# File 're.c', line 3485
VALUE
rb_reg_equal(VALUE re1, VALUE re2)
{
if (re1 == re2) return Qtrue;
if (!RB_TYPE_P(re2, T_REGEXP)) return Qfalse;
rb_reg_check(re1); rb_reg_check(re2);
if (FL_TEST(re1, KCODE_FIXED) != FL_TEST(re2, KCODE_FIXED)) return Qfalse;
if (RREGEXP_PTR(re1)->options != RREGEXP_PTR(re2)->options) return Qfalse;
if (RREGEXP_SRC_LEN(re1) != RREGEXP_SRC_LEN(re2)) return Qfalse;
if (ENCODING_GET(re1) != ENCODING_GET(re2)) return Qfalse;
return RBOOL(memcmp(RREGEXP_SRC_PTR(re1), RREGEXP_SRC_PTR(re2), RREGEXP_SRC_LEN(re1)) == 0);
}
|
#===(string) ⇒ Boolean
Returns true
if self
finds a match in string
:
/^[a-z]*$/ === 'HELLO' # => false
/^[A-Z]*$/ === 'HELLO' # => true
This method is called in case statements:
s = 'HELLO'
case s
when /\A[a-z]*\z/; print "Lower case\n"
when /\A[A-Z]*\z/; print "Upper case\n"
else print "Mixed case\n"
end # => "Upper case"
3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 |
# File 're.c', line 3675
static VALUE
rb_reg_eqq(VALUE re, VALUE str)
{
long start;
str = reg_operand(str, FALSE);
if (NIL_P(str)) {
rb_backref_set(Qnil);
return Qfalse;
}
start = rb_reg_search(re, str, 0, 0);
return RBOOL(start >= 0);
}
|
#=~(string) ⇒ Integer?
Returns the integer index (in characters) of the first match for self
and string
, or nil
if none; also sets the rdoc-ref:Regexp global variables:
/at/ =~ 'input data' # => 7
$~ # => #<MatchData "at">
/ax/ =~ 'input data' # => nil
$~ # => nil
Assigns named captures to local variables of the same names if and only if self
:
-
Is a regexp literal; see Regexp Literals.
-
Does not contain interpolations; see Regexp interpolation.
-
Is at the left of the expression.
Example:
/(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ ' x = y '
p lhs # => "x"
p rhs # => "y"
Assigns nil
if not matched:
/(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ ' x = '
p lhs # => nil
p rhs # => nil
Does not make local variable assignments if self
is not a regexp literal:
r = /(?<foo>\w+)\s*=\s*(?<foo>\w+)/
r =~ ' x = y '
p foo # Undefined local variable
p # Undefined local variable
The assignment does not occur if the regexp is not at the left:
' x = y ' =~ /(?<foo>\w+)\s*=\s*(?<foo>\w+)/
p foo, foo # Undefined local variables
A regexp interpolation, #{}
, also disables the assignment:
r = /(?<foo>\w+)/
/(?<foo>\w+)\s*=\s*#{r}/ =~ 'x = y'
p foo # Undefined local variable
3646 3647 3648 3649 3650 3651 3652 3653 |
# File 're.c', line 3646
VALUE
rb_reg_match(VALUE re, VALUE str)
{
long pos = reg_match_pos(re, &str, 0, NULL);
if (pos < 0) return Qnil;
pos = rb_str_sublen(str, pos);
return LONG2FIX(pos);
}
|
#casefold? ⇒ Boolean
Returns true
if the case-insensitivity flag in self
is set, false
otherwise:
/a/.casefold? # => false
/a/i.casefold? # => true
/(?i:a)/.casefold? # => false
744 745 746 747 748 749 |
# File 're.c', line 744
static VALUE
rb_reg_casefold_p(VALUE re)
{
rb_reg_check(re);
return RBOOL(RREGEXP_PTR(re)->options & ONIG_OPTION_IGNORECASE);
}
|
#encoding ⇒ Encoding
Returns the Encoding object that represents the encoding of obj.
1146 1147 1148 1149 1150 1151 1152 1153 1154 |
# File 'encoding.c', line 1146
VALUE
rb_obj_encoding(VALUE obj)
{
int idx = rb_enc_get_index(obj);
if (idx < 0) {
rb_raise(rb_eTypeError, "unknown encoding");
}
return rb_enc_from_encoding_index(idx & ENC_INDEX_MASK);
}
|
#==(object) ⇒ Boolean
3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 |
# File 're.c', line 3485
VALUE
rb_reg_equal(VALUE re1, VALUE re2)
{
if (re1 == re2) return Qtrue;
if (!RB_TYPE_P(re2, T_REGEXP)) return Qfalse;
rb_reg_check(re1); rb_reg_check(re2);
if (FL_TEST(re1, KCODE_FIXED) != FL_TEST(re2, KCODE_FIXED)) return Qfalse;
if (RREGEXP_PTR(re1)->options != RREGEXP_PTR(re2)->options) return Qfalse;
if (RREGEXP_SRC_LEN(re1) != RREGEXP_SRC_LEN(re2)) return Qfalse;
if (ENCODING_GET(re1) != ENCODING_GET(re2)) return Qfalse;
return RBOOL(memcmp(RREGEXP_SRC_PTR(re1), RREGEXP_SRC_PTR(re2), RREGEXP_SRC_LEN(re1)) == 0);
}
|
#fixed_encoding? ⇒ Boolean
Returns false
if self
is applicable to a string with any ASCII-compatible encoding; otherwise returns true
:
r = /a/ # => /a/
r.fixed_encoding? # => false
r.match?("\u{6666} a") # => true
r.match?("\xa1\xa2 a".force_encoding("euc-jp")) # => true
r.match?("abc".force_encoding("euc-jp")) # => true
r = /a/u # => /a/
r.fixed_encoding? # => true
r.match?("\u{6666} a") # => true
r.match?("\xa1\xa2".force_encoding("euc-jp")) # Raises exception.
r.match?("abc".force_encoding("euc-jp")) # => true
r = /\u{6666}/ # => /\u{6666}/
r.fixed_encoding? # => true
r.encoding # => #<Encoding:UTF-8>
r.match?("\u{6666} a") # => true
r.match?("\xa1\xa2".force_encoding("euc-jp")) # Raises exception.
r.match?("abc".force_encoding("euc-jp")) # => false
1516 1517 1518 1519 1520 |
# File 're.c', line 1516
static VALUE
rb_reg_fixed_encoding_p(VALUE re)
{
return RBOOL(FL_TEST(re, KCODE_FIXED));
}
|
#hash ⇒ Integer
Returns the integer hash value for self
.
Related: Object#hash.
3452 3453 3454 3455 3456 3457 |
# File 're.c', line 3452
VALUE
rb_reg_hash(VALUE re)
{
st_index_t hashval = reg_hash(re);
return ST2FIX(hashval);
}
|
#initialize_copy(re) ⇒ Object
:nodoc:
4385 4386 4387 4388 4389 4390 4391 |
# File 're.c', line 4385
static VALUE
rb_reg_init_copy(VALUE copy, VALUE re)
{
if (!OBJ_INIT_COPY(copy, re)) return copy;
rb_reg_check(re);
return reg_copy(copy, re);
}
|
#inspect ⇒ String
Returns a nicely-formatted string representation of self
:
/ab+c/ix.inspect # => "/ab+c/ix"
Related: Regexp#to_s.
526 527 528 529 530 531 532 533 |
# File 're.c', line 526
static VALUE
rb_reg_inspect(VALUE re)
{
if (!RREGEXP_PTR(re) || !RREGEXP_SRC(re) || !RREGEXP_SRC_PTR(re)) {
return rb_any_to_s(re);
}
return rb_reg_desc(re);
}
|
#match(string, offset = 0) ⇒ MatchData? #match(string, offset = 0) {|matchdata| ... } ⇒ Object
With no block given, returns the MatchData object that describes the match, if any, or nil
if none; the search begins at the given character offset
in string
:
/abra/.match('abracadabra') # => #<MatchData "abra">
/abra/.match('abracadabra', 4) # => #<MatchData "abra">
/abra/.match('abracadabra', 8) # => nil
/abra/.match('abracadabra', 800) # => nil
string = "\u{5d0 5d1 5e8 5d0}cadabra"
/abra/.match(string, 7) #=> #<MatchData "abra">
/abra/.match(string, 8) #=> nil
/abra/.match(string.b, 8) #=> #<MatchData "abra">
With a block given, calls the block if and only if a match is found; returns the block’s value:
/abra/.match('abracadabra') {|matchdata| p matchdata }
# => #<MatchData "abra">
/abra/.match('abracadabra', 4) {|matchdata| p matchdata }
# => #<MatchData "abra">
/abra/.match('abracadabra', 8) {|matchdata| p matchdata }
# => nil
/abra/.match('abracadabra', 8) {|marchdata| fail 'Cannot happen' }
# => nil
Output (from the first two blocks above):
#<MatchData "abra">
#<MatchData "abra">
/(.)(.)(.)/.match("abc")[2] # => "b"
/(.)(.)/.match("abc", 1)[2] # => "c"
3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 |
# File 're.c', line 3762
static VALUE
rb_reg_match_m(int argc, VALUE *argv, VALUE re)
{
VALUE result = Qnil, str, initpos;
long pos;
if (rb_scan_args(argc, argv, "11", &str, &initpos) == 2) {
pos = NUM2LONG(initpos);
}
else {
pos = 0;
}
pos = reg_match_pos(re, &str, pos, &result);
if (pos < 0) {
rb_backref_set(Qnil);
return Qnil;
}
rb_match_busy(result);
if (!NIL_P(result) && rb_block_given_p()) {
return rb_yield(result);
}
return result;
}
|
#match?(string) ⇒ Boolean #match?(string, offset = 0) ⇒ Boolean
Returns true
or false
to indicate whether the regexp is matched or not without updating $~ and other related variables. If the second parameter is present, it specifies the position in the string to begin the search.
/R.../.match?("Ruby") # => true
/R.../.match?("Ruby", 1) # => false
/P.../.match?("Ruby") # => false
$& # => nil
3803 3804 3805 3806 3807 3808 |
# File 're.c', line 3803
static VALUE
rb_reg_match_m_p(int argc, VALUE *argv, VALUE re)
{
long pos = rb_check_arity(argc, 1, 2) > 1 ? NUM2LONG(argv[1]) : 0;
return rb_reg_match_p(re, argv[0], pos);
}
|
#named_captures ⇒ Hash
Returns a hash representing named captures of self
(see Named Captures):
-
Each key is the name of a named capture.
-
Each value is an array of integer indexes for that named capture.
Examples:
/(?<foo>.)(?<bar>.)/.named_captures # => {"foo"=>[1], "bar"=>[2]}
/(?<foo>.)(?<foo>.)/.named_captures # => {"foo"=>[1, 2]}
/(.)(.)/.named_captures # => {}
862 863 864 865 866 867 868 869 |
# File 're.c', line 862
static VALUE
rb_reg_named_captures(VALUE re)
{
regex_t *reg = (rb_reg_check(re), RREGEXP_PTR(re));
VALUE hash = rb_hash_new_with_size(onig_number_of_names(reg));
onig_foreach_name(reg, reg_named_captures_iter, (void*)hash);
return hash;
}
|
#names ⇒ Object
Returns an array of names of captures (see Named Captures):
/(?<foo>.)(?<bar>.)(?<baz>.)/.names # => ["foo", "bar", "baz"]
/(?<foo>.)(?<foo>.)/.names # => ["foo"]
/(.)(.)/.names # => []
818 819 820 821 822 823 824 825 826 |
# File 're.c', line 818
static VALUE
rb_reg_names(VALUE re)
{
VALUE ary;
rb_reg_check(re);
ary = rb_ary_new_capa(onig_number_of_names(RREGEXP_PTR(re)));
onig_foreach_name(RREGEXP_PTR(re), reg_names_iter, (void*)ary);
return ary;
}
|
#options ⇒ Integer
Returns an integer whose bits show the options set in self
.
The option bits are:
Regexp::IGNORECASE # => 1
Regexp::EXTENDED # => 2
Regexp::MULTILINE # => 4
Examples:
/foo/. # => 0
/foo/i. # => 1
/foo/x. # => 2
/foo/m. # => 4
/foo/mix. # => 7
Note that additional bits may be set in the returned integer; these are maintained internally in self
, are ignored if passed to Regexp.new, and may be ignored by the caller:
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:
r = /\xa1\xa2/e # => /\xa1\xa2/
r.source # => "\\xa1\\xa2"
r. # => 16
Regexp.new(r.source, r.) # => /\xa1\xa2/
789 790 791 792 793 794 |
# File 're.c', line 789
static VALUE
rb_reg_options_m(VALUE re)
{
int options = rb_reg_options(re);
return INT2NUM(options);
}
|
#source ⇒ String
Returns the original string of self
:
/ab+c/ix.source # => "ab+c"
Regexp escape sequences are retained:
/\x20\+/.source # => "\\x20\\+"
Lexer escape characters are not retained:
/\//.source # => "/"
505 506 507 508 509 510 511 512 513 |
# File 're.c', line 505
static VALUE
rb_reg_source(VALUE re)
{
VALUE str;
rb_reg_check(re);
str = rb_str_dup(RREGEXP_SRC(re));
return str;
}
|
#timeout ⇒ Float?
It returns the timeout interval for Regexp matching in second. nil
means no default timeout configuration.
This configuration is per-object. The global configuration set by Regexp.timeout= is ignored if per-object configuration is set.
re = Regexp.new("^a*b?a*$", timeout: 1)
re.timeout #=> 1.0
re =~ "a" * 100000 + "x" #=> regexp match timeout (RuntimeError)
4702 4703 4704 4705 4706 4707 4708 4709 |
# File 're.c', line 4702
static VALUE
rb_reg_timeout_get(VALUE re)
{
rb_reg_check(re);
double d = hrtime2double(RREGEXP_PTR(re)->timelimit);
if (d == 0.0) return Qnil;
return DBL2NUM(d);
}
|
#to_s ⇒ String
Returns a string showing the options and string of self
:
r0 = /ab+c/ix
s0 = r0.to_s # => "(?ix-m:ab+c)"
The returned string may be used as an argument to Regexp.new, or as interpolated text for a Regexp interpolation:
r1 = Regexp.new(s0) # => /(?ix-m:ab+c)/
r2 = /#{s0}/ # => /(?ix-m:ab+c)/
Note that r1
and r2
are not equal to r0
because their original strings are different:
r0 == r1 # => false
r0.source # => "ab+c"
r1.source # => "(?ix-m:ab+c)"
Related: Regexp#inspect.
564 565 566 567 568 |
# File 're.c', line 564
static VALUE
rb_reg_to_s(VALUE re)
{
return rb_reg_str_with_term(re, '/');
}
|
#~(rxp) ⇒ Integer?
Equivalent to rxp =~ $_
:
$_ = "input data"
~ /at/ # => 7
3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 |
# File 're.c', line 3701
VALUE
rb_reg_match2(VALUE re)
{
long start;
VALUE line = rb_lastline_get();
if (!RB_TYPE_P(line, T_STRING)) {
rb_backref_set(Qnil);
return Qnil;
}
start = rb_reg_search(re, line, 0, 0);
if (start < 0) {
return Qnil;
}
start = rb_str_sublen(line, start);
return LONG2FIX(start);
}
|