Package scala.util.matching
package matching
Type Members
class Regex extends Serializable
trait UnanchoredRegex extends Regex
A Regex that finds the first match when used in a pattern match.
- See also
Value Members
object Regex extends java.io.Serializable
© 2002-2019 EPFL, with contributions from Lightbend.
Licensed under the Apache License, Version 2.0.
https://www.scala-lang.org/api/2.13.0/scala/util/matching/index.html
A regular expression is used to determine whether a string matches a pattern and, if it does, to extract or transform the parts that match.
Usage
This class delegates to the java.util.regex package of the Java Platform. See the documentation for java.util.regex.Pattern for details about the regular expression syntax for pattern strings.
An instance of
Regex
represents a compiled regular expression pattern. Since compilation is expensive, frequently usedRegex
es should be constructed once, outside of loops and perhaps in a companion object.The canonical way to create a
Regex
is by using the methodr
, provided implicitly for strings:Since escapes are not processed in multi-line string literals, using triple quotes avoids having to escape the backslash character, so that
"\\d"
can be written"""\d"""
. The same result is achieved with certain interpolators, such asraw"\d".r
or a custom interpolatorr"\d"
that also compiles theRegex
.Extraction
To extract the capturing groups when a
Regex
is matched, use it as an extractor in a pattern match:To check only whether the
Regex
matches, ignoring any groups, use a sequence wildcard:That works because a
Regex
extractor produces a sequence of strings. Extracting only the year from a date could also be expressed with a sequence wildcard:In a pattern match,
Regex
normally matches the entire input. However, an unanchoredRegex
finds the pattern anywhere in the input.Find Matches
To find or replace matches of the pattern, use the various find and replace methods. For each method, there is a version for working with matched strings and another for working with
Match
objects.For example, pattern matching with an unanchored
Regex
, as in the previous example, can also be accomplished usingfindFirstMatchIn
. ThefindFirst
methods return anOption
which is non-empty if a match is found, orNone
for no match:To find all matches:
To check whether input is matched by the regex:
To iterate over the matched strings, use
findAllIn
, which returns a special iterator that can be queried for theMatchData
of the last match:Although the
MatchIterator
returned byfindAllIn
is used like anyIterator
, with alternating calls tohasNext
andnext
,hasNext
has the additional side effect of advancing the underlying matcher to the next unconsumed match. This effect is visible in theMatchData
representing the "current match".The example shows that methods on
MatchData
such asstart
will advance to the first match, if necessary. It also shows thathasNext
will advance to the next unconsumed match, ifnext
has already returned the current match.The current
MatchData
can be captured using thematchData
method. Alternatively,findAllMatchIn
returns anIterator[Match]
, where there is no interaction between the iterator andMatch
objects it has already produced.Note that
findAllIn
finds matches that don't overlap. (See findAllIn for more examples.)Replace Text
Text replacement can be performed unconditionally or as a function of the current match:
Pattern matching the
Match
against theRegex
that created it does not reapply theRegex
. In the expression forreformatted
, eachdate
match is computed once. But it is possible to apply aRegex
to aMatch
resulting from a different pattern:java.util.regex.Pattern