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.
EXTENDED |
Ignore spaces and newlines in regexp. |
IGNORECASE |
Matches are case insensitive. |
MULTILINE |
Newlines treated as any other character. |
Regexp.new
.Regexp.escape(str)=~str
will be true.
Regexp.escape('\\*?{}.') → \\\\\*\?\{\}\.
MatchData
object generated by the last successful pattern match. Equivalent to reading the global variable $~. See the reference for the MatchData
class.String
or a Regexp
(in which case that regexp's options are not propagated). If options is a Fixnum
, it should be one or more of the constants Regexp::EXTENDED
, Regexp::IGNORECASE
, and Regexp::POSIXLINE
, 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(r1, true) → /^a-z+:\s+\w+/i
r3 = Regexp.new(r2, Regexp::EXTENDED) → /^a-z+:\s+\w+/x
Regexp.escape
.true
or false
casefold?
values are the same.
/abc/ == /abc/x → false
/abc/ == /abc/i → false
/abc/u == /abc/n → false
true
or false
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
nil
nil
if the match failed.
/SIT/ =~ "insensitive" → nil
/SIT/i =~ "insensitive" → 5
nil
$_
. Equivalent to
rxp =~ $_
.
$_ = "input data"
~ /at/ → 7
true
or false
nil
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"
/ab+c/ix.source → "ab+c"
Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide"
Copyright © 2001 by Addison Wesley Longman, Inc. This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/).
Distribution of substantively modified versions of this document is prohibited without the explicit permission of the copyright holder.
Distribution of the work or derivative of the work in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder.