package matching
- Alphabetic
- Public
- All
Type Members
-
class
Regex
extends Serializable
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.
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:val date = """(\d\d\d\d)-(\d\d)-(\d\d)""".r
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"""
.Extraction
To extract the capturing groups when a
Regex
is matched, use it as an extractor in a pattern match:"2004-01-20" match { case date(year, month, day) => s"$year was a good year for PLs." }
To check only whether the
Regex
matches, ignoring any groups, use a sequence wildcard:"2004-01-20" match { case date(_*) => "It's a date!" }
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:"2004-01-20" match { case date(year, _*) => s"$year was a good year for PLs." }
In a pattern match,
Regex
normally matches the entire input. However, an unanchoredRegex
finds the pattern anywhere in the input.val embeddedDate = date.unanchored "Date: 2004-01-20 17:25:18 GMT (10 years, 28 weeks, 5 days, 17 hours and 51 minutes ago)" match { case embeddedDate("2004", "01", "20") => "A Scala is born." }
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:val dates = "Important dates in history: 2004-01-20, 1958-09-05, 2010-10-06, 2011-07-15" val firstDate = date.findFirstIn(dates).getOrElse("No date found.") val firstYear = for (m <- date.findFirstMatchIn(dates)) yield m.group(1)
To find all matches:
val allYears = for (m <- date.findAllMatchIn(dates)) yield m.group(1)
To iterate over the matched strings, use
findAllIn
, which returns a special iterator that can be queried for theMatchData
of the last match:val mi = date.findAllIn(dates) while (mi.hasNext) { val d = mi.next if (mi.group(1).toInt < 1960) println(s"$d: An oldie but goodie.")
Note that
findAllIn
finds matches that don't overlap. (See findAllIn for more examples.)val num = """(\d+)""".r val all = num.findAllIn("123").toList // List("123"), not List("123", "23", "3")
Also, the "current match" of a
MatchIterator
may be advanced by eitherhasNext
ornext
. By comparison, theIterator[Match]
returned byfindAllMatchIn
orfindAllIn.matchData
producesMatch
objects that remain valid after the iterator is advanced.val ns = num.findAllIn("1 2 3") ns.start // 0 ns.hasNext // true ns.start // 2 val ms = num.findAllMatchIn("1 2 3") val m = ms.next() m.start // 0 ms.hasNext // true m.start // still 0
Replace Text
Text replacement can be performed unconditionally or as a function of the current match:
val redacted = date.replaceAllIn(dates, "XXXX-XX-XX") val yearsOnly = date.replaceAllIn(dates, m => m.group(1)) val months = (0 to 11).map { i => val c = Calendar.getInstance; c.set(2014, i, 1); f"$c%tb" } val reformatted = date.replaceAllIn(dates, _ match { case date(y,m,d) => f"${months(m.toInt - 1)} $d, $y" })
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:val docSpree = """2011(?:-\d{2}){2}""".r val docView = date.replaceAllIn(dates, _ match { case docSpree() => "Historic doc spree!" case _ => "Something else happened" })
- Annotations
- @SerialVersionUID()
- Version
1.1, 29/01/2008
- See also
java.util.regex.Pattern
-
trait
UnanchoredRegex
extends Regex
A Regex that finds the first match when used in a pattern match.
A Regex that finds the first match when used in a pattern match.
- See also
Value Members
-
object
Regex
extends Serializable
This object defines inner classes that describe regex matches and helper objects.
This is the documentation for the Scala standard library.
Package structure
The scala package contains core types like
Int
,Float
,Array
orOption
which are accessible in all Scala compilation units without explicit qualification or imports.Notable packages include:
scala.collection
and its sub-packages contain Scala's collections frameworkscala.collection.immutable
- Immutable, sequential data-structures such asVector
,List
,Range
,HashMap
orHashSet
scala.collection.mutable
- Mutable, sequential data-structures such asArrayBuffer
,StringBuilder
,HashMap
orHashSet
scala.collection.concurrent
- Mutable, concurrent data-structures such asTrieMap
scala.collection.parallel.immutable
- Immutable, parallel data-structures such asParVector
,ParRange
,ParHashMap
orParHashSet
scala.collection.parallel.mutable
- Mutable, parallel data-structures such asParArray
,ParHashMap
,ParTrieMap
orParHashSet
scala.concurrent
- Primitives for concurrent programming such asFutures
andPromises
scala.io
- Input and output operationsscala.math
- Basic math functions and additional numeric types likeBigInt
andBigDecimal
scala.sys
- Interaction with other processes and the operating systemscala.util.matching
- Regular expressionsOther packages exist. See the complete list on the right.
Additional parts of the standard library are shipped as separate libraries. These include:
scala.reflect
- Scala's reflection API (scala-reflect.jar)scala.xml
- XML parsing, manipulation, and serialization (scala-xml.jar)scala.swing
- A convenient wrapper around Java's GUI framework called Swing (scala-swing.jar)scala.util.parsing
- Parser combinators, including an example implementation of a JSON parser (scala-parser-combinators.jar)Automatic imports
Identifiers in the scala package and the
scala.Predef
object are always in scope by default.Some of these identifiers are type aliases provided as shortcuts to commonly used classes. For example,
List
is an alias forscala.collection.immutable.List
.Other aliases refer to classes provided by the underlying platform. For example, on the JVM,
String
is an alias forjava.lang.String
.