Regex

object Regex

This object defines inner classes that describe regex matches and helper objects.

Companion
class
Source
Regex.scala
class Object
trait Matchable
class Any

Type members

Classlikes

object Groups

An extractor object that yields the groups in the match. Using this extractor rather than the original Regex ensures that the match is not recomputed.

import scala.util.matching.Regex.Groups val date = """(\d\d\d\d)-(\d\d)-(\d\d)""".r val text = "The doc spree happened on 2011-07-15." val day = date replaceAllIn(text, _ match { case Groups(_, month, day) => s"$month/$day" })
class Match(val source: CharSequence, val matcher: Matcher, val groupNames: Seq[String]) extends MatchData

Provides information about a successful match.

Companion
object
Source
Regex.scala
object Match

An extractor object for Matches, yielding the matched string.

This can be used to help writing replacer functions when you are not interested in match data. For example:

import scala.util.matching.Regex.Match """\w+""".r replaceAllIn ("A simple example.", _ match { case Match(s) => s.toUpperCase })
Companion
class
Source
Regex.scala
trait MatchData

This class provides methods to access the details of a match.

class MatchIterator(val source: CharSequence, val regex: Regex, val groupNames: Seq[String]) extends AbstractIterator[String] with Iterator[String] with MatchData

A class to step through a sequence of regex matches.

This is an iterator that returns the matched strings.

Queries about match data pertain to the current state of the underlying matcher, which is advanced by calling hasNext or next.

When matches are exhausted, queries about match data will throw java.lang.IllegalStateException.

Value members

Concrete methods

def quote(text: String): String

Quotes strings to be used literally in regex patterns.

All regex metacharacters in the input match themselves literally in the output.

Example

List("US$", "CAN$").map(Regex.quote).mkString("|").r
Source
Regex.scala

Quotes replacement strings to be used in replacement methods.

Replacement methods give special meaning to backslashes (\) and dollar signs ($) in replacement strings, so they are not treated as literals. This method escapes these characters so the resulting string can be used as a literal replacement representing the input string.

Value Params
text

The string one wishes to use as literal replacement.

Returns

A string that can be used to replace matches with text.

Example

"CURRENCY".r.replaceAllIn(input, Regex quoteReplacement "US$")
Source
Regex.scala