Movatterモバイル変換


[0]ホーム

URL:


Scala 3
3.7.4
LearnInstallPlaygroundFind A LibraryCommunityBlog
Scala 3
LearnInstallPlaygroundFind A LibraryCommunityBlog
DocsAPI
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL
Scala 3/scala/scala.util/scala.util.matching/Regex

Regex

scala.util.matching.Regex
See theRegex companion object
classRegex extendsSerializable

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 thejava.util.regex package of the Java Platform. See the documentation forjava.util.regex.Pattern for details about the regular expression syntax for pattern strings.

An instance ofRegex represents a compiled regular expression pattern. Since compilation is expensive, frequently usedRegexes should be constructed once, outside of loops and perhaps in a companion object.

The canonical way to create aRegex is by using the methodr, provided implicitly for strings:

val date = raw"(\d{4})-(\d{2})-(\d{2})".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""". 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 aRegex 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 theRegex matches, ignoring any groups, use a sequence wildcard:

"2004-01-20" match {  case date(_*) => "It's a date!"}

That works because aRegex 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 withMatch objects.

For example, pattern matching with an unanchoredRegex, 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 check whether input is matched by the regex:

date.matches("2018-03-01")                     // truedate.matches("Today is 2018-03-01")            // falsedate.unanchored.matches("Today is 2018-03-01") // true

To iterate over the matched strings, usefindAllIn, 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.")}

Although theMatchIterator 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".

val r = "(ab+c)".rval s = "xxxabcyyyabbczzz"r.findAllIn(s).start    // 3val mi = r.findAllIn(s)mi.hasNext              // truemi.start                // 3mi.next()               // "abc"mi.start                // 3mi.hasNext              // truemi.start                // 9mi.next()               // "abbc"

The example shows that methods onMatchData 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 currentMatchData 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 thatfindAllIn finds matches that don't overlap. (SeefindAllIn for more examples.)

val num = raw"(\d+)".rval all = num.findAllIn("123").toList  // List("123"), not List("123", "23", "3")

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 theMatch 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}""".rval docView  = date.replaceAllIn(dates, _ match {  case docSpree() => "Historic doc spree!"  case _          => "Something else happened"})

Value parameters

groupNames

A mapping from names to indices in capture groups

pattern

The compiled pattern

Attributes

See also
Companion
object
Source
Regex.scala
Graph
Supertypes
classObject
traitMatchable
classAny
Known subtypes
Self type

Members list

Value members

Constructors

defthis(regex:String,groupNames:String*)

Compile a regular expression, supplied as a string, into a pattern that can be matched against inputs.

Compile a regular expression, supplied as a string, into a pattern that can be matched against inputs.

If group names are supplied, they can be used this way:

val namedDate  = new Regex("""(\d\d\d\d)-(\d\d)-(\d\d)""", "year", "month", "day")val namedYears = for (m <- namedDate findAllMatchIn dates) yield m group "year"

Inline group names are preferred over group names supplied to the constructor when retrieving matched groups by name. Group names supplied to the constructor should be considered deprecated.

This constructor does not support options as flags, which must be supplied as inline flags in the pattern string:(?idmsuxU).

Value parameters

groupNames

Names of capturing groups.

regex

The regular expression to compile.

Attributes

Source
Regex.scala

Concrete methods

Attributes

Source
Regex.scala

Return all non-overlapping matches of thisRegex in the given character sequence as ascala.util.matching.Regex.MatchIterator, which is a specialscala.collection.Iterator that returns the matched strings but can also be queried for more data about the last match, such as capturing groups and start position.

Return all non-overlapping matches of thisRegex in the given character sequence as ascala.util.matching.Regex.MatchIterator, which is a specialscala.collection.Iterator that returns the matched strings but can also be queried for more data about the last match, such as capturing groups and start position.

AMatchIterator can also be converted into an iterator that returns objects of typescala.util.matching.Regex.Match, such as is normally returned byfindAllMatchIn.

Where potential matches overlap, the first possible match is returned, followed by the next match that follows the input consumed by the first match:

val hat  = "hat[^a]+".rval hathaway = "hathatthattthatttt"val hats = hat.findAllIn(hathaway).toList                     // List(hath, hattth)val pos  = hat.findAllMatchIn(hathaway).map(_.start).toList   // List(0, 7)

To return overlapping matches, it is possible to formulate a regular expression with lookahead (?=) that does not consume the overlapping region.

val madhatter = "(h)(?=(at[^a]+))".rval madhats   = madhatter.findAllMatchIn(hathaway).map {  case madhatter(x,y) => s"$x$y"}.toList                                       // List(hath, hatth, hattth, hatttt)

Attempting to retrieve match information after exhausting the iterator results injava.lang.IllegalStateException. Seescala.util.matching.Regex.MatchIterator for details.

Value parameters

source

The text to match against.

Attributes

Returns

Ascala.util.matching.Regex.MatchIterator of matched substrings.

Example

for (words <- """\w+""".r findAllIn "A simple example.") yield words
Source
Regex.scala

Return all non-overlapping matches of this regexp in given character sequence as ascala.collection.Iterator ofscala.util.matching.Regex.Match.

Return all non-overlapping matches of this regexp in given character sequence as ascala.collection.Iterator ofscala.util.matching.Regex.Match.

Value parameters

source

The text to match against.

Attributes

Returns

Ascala.collection.Iterator ofscala.util.matching.Regex.Match for all matches.

Example

for (words <- """\w+""".r findAllMatchIn "A simple example.") yield words.start
Source
Regex.scala

Return an optional first matching string of thisRegex in the given character sequence, or None if there is no match.

Return an optional first matching string of thisRegex in the given character sequence, or None if there is no match.

Value parameters

source

The text to match against.

Attributes

Returns

Anscala.Option of the first matching string in the text.

Example

"""\w+""".r findFirstIn "A simple example." foreach println // prints "A"
Source
Regex.scala

Return an optional first match of thisRegex in the given character sequence, or None if it does not exist.

Return an optional first match of thisRegex in the given character sequence, or None if it does not exist.

If the match is successful, thescala.util.matching.Regex.Match can be queried for more data.

Value parameters

source

The text to match against.

Attributes

Returns

Ascala.Option ofscala.util.matching.Regex.Match of the first matching string in the text.

Example

("""[a-z]""".r findFirstMatchIn "A simple example.") map (_.start) // returns Some(2), the index of the first match in the text
Source
Regex.scala

Return an optional match of thisRegex at the beginning of the given character sequence, or None if it matches no prefix of the character sequence.

Return an optional match of thisRegex at the beginning of the given character sequence, or None if it matches no prefix of the character sequence.

UnlikefindFirstMatchIn, this method will only return a match at the beginning of the input.

Value parameters

source

The text to match against.

Attributes

Returns

Ascala.Option of thescala.util.matching.Regex.Match of the matched string.

Example

"""\w+""".r findPrefixMatchOf "A simple example." map (_.after) // returns Some(" simple example.")
Source
Regex.scala

Return an optional match of thisRegex at the beginning of the given character sequence, or None if it matches no prefix of the character sequence.

Return an optional match of thisRegex at the beginning of the given character sequence, or None if it matches no prefix of the character sequence.

UnlikefindFirstIn, this method will only return a match at the beginning of the input.

Value parameters

source

The text to match against.

Attributes

Returns

Ascala.Option of the matched prefix.

Example

"""\p{Lower}""".r findPrefixOf "A simple example." // returns None, since the text does not begin with a lowercase letter
Source
Regex.scala

Returns whether thisRegex matches the given character sequence.

Returns whether thisRegex matches the given character sequence.

Like the extractor, this method takes anchoring into account.

Value parameters

source

The text to match against

Attributes

Returns

true if and only ifsource matches thisRegex.

See also
Example

"""\d+""".r matches "123" // returns true
Source
Regex.scala

Attributes

Source
Regex.scala
defreplaceAllIn(target:CharSequence,replacement:String):String

Replaces all matches by a string.

Replaces all matches by a string.

In the replacement String, a dollar sign ($) followed by a number will be interpreted as a reference to a group in the matched pattern, with numbers 1 through 9 corresponding to the first nine groups, and 0 standing for the whole match. Any other character is an error. The backslash (\) character will be interpreted as an escape character and can be used to escape the dollar sign. UseRegex.quoteReplacement to escape these characters.

Value parameters

replacement

The string that will replace each match

target

The string to match

Attributes

Returns

The resulting string

Example

"""\d+""".r replaceAllIn ("July 15", "<NUMBER>") // returns "July <NUMBER>"
Source
Regex.scala

Replaces all matches using a replacer function.

Replaces all matches using a replacer function. The replacer function takes ascala.util.matching.Regex.Match so that extra information can be obtained from the match. For example:

import scala.util.matching.Regexval datePattern = new Regex("""(\d\d\d\d)-(\d\d)-(\d\d)""", "year", "month", "day")val text = "From 2011-07-15 to 2011-07-17"val repl = datePattern replaceAllIn (text, m => s"${m group "month"}/${m group "day"}")

In the replacement String, a dollar sign ($) followed by a number will be interpreted as a reference to a group in the matched pattern, with numbers 1 through 9 corresponding to the first nine groups, and 0 standing for the whole match. Any other character is an error. The backslash (\) character will be interpreted as an escape character and can be used to escape the dollar sign. UseRegex.quoteReplacement to escape these characters.

Value parameters

replacer

The function which maps a match to another string.

target

The string to match.

Attributes

Returns

The target string after replacements.

Source
Regex.scala
defreplaceFirstIn(target:CharSequence,replacement:String):String

Replaces the first match by a string.

Replaces the first match by a string.

In the replacement String, a dollar sign ($) followed by a number will be interpreted as a reference to a group in the matched pattern, with numbers 1 through 9 corresponding to the first nine groups, and 0 standing for the whole match. Any other character is an error. The backslash (\) character will be interpreted as an escape character and can be used to escape the dollar sign. UseRegex.quoteReplacement to escape these characters.

Value parameters

replacement

The string that will replace the match

target

The string to match

Attributes

Returns

The resulting string

Source
Regex.scala

Replaces some of the matches using a replacer function that returns anscala.Option.

Replaces some of the matches using a replacer function that returns anscala.Option. The replacer function takes ascala.util.matching.Regex.Match so that extra information can be obtained from the match. For example:

import scala.util.matching.Regex._val vars = Map("x" -> "a var", "y" -> """some $ and \ signs""")val text = "A text with variables %x, %y and %z."val varPattern = """%(\w+)""".rval mapper = (m: Match) => vars get (m group 1) map (quoteReplacement(_))val repl = varPattern replaceSomeIn (text, mapper)

In the replacement String, a dollar sign ($) followed by a number will be interpreted as a reference to a group in the matched pattern, with numbers 1 through 9 corresponding to the first nine groups, and 0 standing for the whole match. Any other character is an error. The backslash (\) character will be interpreted as an escape character and can be used to escape the dollar sign. UseRegex.quoteReplacement to escape these characters.

Value parameters

replacer

The function which optionally maps a match to another string.

target

The string to match.

Attributes

Returns

The target string after replacements.

Source
Regex.scala

Splits the provided character sequence around matches of this regexp.

Splits the provided character sequence around matches of this regexp.

Value parameters

toSplit

The character sequence to split

Attributes

Returns

The array of strings computed by splitting the input around matches of this regexp

Source
Regex.scala
overridedeftoString:String

The string defining the regular expression

The string defining the regular expression

Attributes

Definition Classes
Source
Regex.scala

Create a new Regex with the same pattern, but no requirement that the entire String matches in extractor patterns andRegex#matches.

Create a new Regex with the same pattern, but no requirement that the entire String matches in extractor patterns andRegex#matches.

Normally, matching ondate behaves as though the pattern were enclosed in anchors,"^pattern$".

The unanchoredRegex behaves as though those anchors were removed.

Note that this method does not actually strip any matchers from the pattern.

Callinganchored returns the originalRegex.

val date = """(\d\d\d\d)-(\d\d)-(\d\d)""".r.unanchoredval date(year, month, day) = "Date 2011-07-15"                       // OKval copyright: String = "Date of this document: 2011-07-15" match {  case date(year, month, day) => s"Copyright $year"                  // OK  case _                      => "No copyright"}

Attributes

Returns

The new unanchored regex

Source
Regex.scala

Tries to match ajava.lang.CharSequence.

Tries to match ajava.lang.CharSequence.

If the match succeeds, the result is a list of the matching groups (or anull element if a group did not match any input). If the pattern specifies no groups, then the result will be an empty list on a successful match.

This method attempts to match the entire input by default; to find the next matching subsequence, use an unanchoredRegex.

For example:

val p1 = "ab*c".rval p1Matches = "abbbc" match {  case p1() => true               // no groups  case _    => false}val p2 = "a(b*)c".rval p2Matches = "abbbc" match {  case p2(_*) => true             // any groups  case _      => false}val numberOfB = "abbbc" match {  case p2(b) => Some(b.length)    // one group  case _     => None}val p3 = "b*".r.unanchoredval p3Matches = "abbbc" match {  case p3() => true               // find the b's  case _    => false}val p4 = "a(b*)(c+)".rval p4Matches = "abbbcc" match {  case p4(_*) => true             // multiple groups  case _      => false}val allGroups = "abbbcc" match {  case p4(all @ _*) => all mkString "/" // "bbb/cc"  case _            => ""}val cGroup = "abbbcc" match {  case p4(_, c) => c  case _        => ""}

Value parameters

s

The string to match

Attributes

Returns

The matches

Source
Regex.scala

Tries to match the String representation of ascala.Char.

Tries to match the String representation of ascala.Char.

If the match succeeds, the result is the first matching group if any groups are defined, or an empty Sequence otherwise.

For example:

val cat = "cat"// the case must consume the group to matchval r = """(\p{Lower})""".rcat(0) match { case r(x) => true }cat(0) match { case r(_) => true }cat(0) match { case r(_*) => true }cat(0) match { case r() => true }     // no match// there is no group to extractval r = """\p{Lower}""".rcat(0) match { case r(x) => true }    // no matchcat(0) match { case r(_) => true }    // no matchcat(0) match { case r(_*) => true }   // matchescat(0) match { case r() => true }     // matches// even if there are multiple groups, only one is returnedval r = """((.))""".rcat(0) match { case r(_) => true }    // matchescat(0) match { case r(_,_) => true }  // no match

Value parameters

c

The Char to match

Attributes

Returns

The match

Source
Regex.scala

Tries to match on ascala.util.matching.Regex.Match.

A previously failed match results in None.

If a successful match was made against the current pattern, then that result is used.

Otherwise, this Regex is applied to the previously matched input, and the result of that match is used.

Attributes

Source
Regex.scala

Concrete fields

Attributes

Source
Regex.scala
In this article
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL

[8]ページ先頭

©2009-2025 Movatter.jp