class Regexp
A regular expression: a literal /.../imx or .new. Searches go
through String's methods (String#match, String#scan,
String#gsub, ...) or this class's match, match? and =~;
positions are character indices. Bytes are searched with the same
patterns, byte by byte.
The syntax is Ruby's, as far as Rust's regex engine has it: no
backtracking, so a search takes time linear in the text.
- Characters, classes
[a-z]([^...], nested,&&intersection),., the escapes\d \w \s \h \band their negations,\p{L},\p{Han}and\P{...}, POSIX classes[[:alpha:]](Unicode, as in Ruby),\x{e9}andé. - Anchors:
^and$at the start and end of every line, as in Ruby;\Aand\zat the text's. - Quantifiers
* + ? {n} {n,} {n,m}and their lazy forms (*?). - Groups
(...),(?:...), named(?<name>...)(a pattern with a named group captures with its named groups only, as in Ruby), and flags(?i),(?i:...),(?-i:...). - Flags:
iignores case (Unicode),mmakes.match a newline,xignores whitespace and#comments.
Unlike Ruby's, \d and \s are Unicode too. Look-ahead and
look-behind, backreferences (\1 in a pattern), atomic groups,
possessive quantifiers, (?#...), \G, \K, \R, \X, \Z and
{,n} are not supported: such a pattern raises ArgumentError when
it is made, which for a literal is when it is evaluated.
/(\d+)-(\d+)/.match?("10-20") # => true
"2024-10-25"[/\d+/] # => "2024"
"aé1"[/\p{L}+/] # => "aé"
"a\nb".match?(/^b$/) # => true
"a\nb".match?(/\Ab/) # => false
Regexp.new("a(?=b)") # raises ArgumentError
<prelude>:2782
Class methods
def self.new(source: String, options: String = …) -> Regexp
The regular expression whose pattern is source, with options the
literal's flags (i, m, x): Regexp.new("a+b", "i") is /a+b/i.
Raises
ArgumentError when source is not a pattern the engine supports
(see Regexp), or for an option other than i, m and x.
Regexp.new("a+b", "i").match?("AAB") # => true
Regexp.new("a(") # raises ArgumentError
<prelude>:2797
def self.escape(text: String) -> String
text with the characters that have a meaning in a pattern escaped
by a backslash, so that it matches itself. Unlike Ruby's, whitespace
is left as it is.
Regexp.escape("a.b*c") # => "a\\.b\\*c"
<prelude>:2805
def self.union(*patterns: Object) -> Regexp
A pattern matching any of patterns: a string matches itself
(escaped), a Regexp its pattern, in a group that keeps its flags
((?i-mx:...)); a single Regexp is returned as it is. With no
pattern, the result is //, which matches everywhere (Ruby's matches
nothing).
Raises
TypeError for a pattern that is neither a String nor a Regexp.
Regexp.union("a.b", /c+/i) # => /a\.b|(?i-mx:c+)/
<prelude>:2819
Methods
def match(text: String) -> MatchData?
The first match in text, or nil when there is none.
/(\d+)/.match("ab12")&.captures # => ["12"]
/x/.match("ab") # => nil
<prelude>:2827
def match?(text: String) -> Bool
Whether the pattern matches somewhere in text.
<prelude>:2829
def =~(text: String) -> Int?
The character index of the first match in text, or nil when there
is none.
<prelude>:2832
def source -> String
The pattern as written, without the slashes and the flags.
/a\/b/i.source # => "a/b"
<prelude>:2838
def options -> String
The flags, as a string of i, m and x (empty for none). Unlike
Ruby's, not an integer.
/a/mi.options # => "mi"
<prelude>:2845
def names -> Array[String]
The names of the named groups, in order.
/(?<y>\d+)-(?<m>\d+)/.names # => ["y", "m"]
<prelude>:2851
def to_s -> String
The pattern as a literal, /a+/i: the same as inspect.
<prelude>:2853
def inspect -> String
The pattern as a literal, /a+/i.
<prelude>:2855
def ==(other: Object) -> Bool
Whether other is a Regexp with the same source and flags.
<prelude>:2857
def hash -> Int
A hash code: equal patterns have equal ones.
<prelude>:2859