scala.util.matching

Regex

class Regex extends Serializable

This class provides methods for creating and using regular expressions. It is based on the regular expressions of the JDK since 1.4.

Its main goal is to extract strings that match a pattern, or the subgroups that make it up. For that reason, it is usually used with for comprehensions and matching (see methods for examples).

A Regex is created from a java.lang.String representation of the regular expression pattern1. That pattern is compiled during construction, so frequently used patterns should be declared outside loops if performance is of concern. Possibly, they might be declared on a companion object, so that they need only to be initialized once.

The canonical way of creating regex patterns is by using the method r, provided on java.lang.String through an implicit conversion into scala.collection.immutable.WrappedString. Using triple quotes to write these strings avoids having to quote the backslash character (\).

Using the constructor directly, on the other hand, makes it possible to declare names for subgroups in the pattern.

For example, both declarations below generate the same regex, but the second one associate names with the subgroups.

val dateP1 = """(\d\d\d\d)-(\d\d)-(\d\d)""".r
val dateP2 = new scala.util.matching.Regex("""(\d\d\d\d)-(\d\d)-(\d\d)""", "year", "month", "day")

There are two ways of using a Regex to find a pattern: calling methods on Regex, such as findFirstIn or findAllIn, or using it as an extractor in a pattern match.

Note, however, that when Regex is used as an extractor in a pattern match, it only succeeds if the whole text can be matched. For this reason, one usually calls a method to find the matching substrings, and then use it as an extractor to break match into subgroups.

As an example, the above patterns can be used like this:

val dateP1(year, month, day) = "2011-07-15"

// val dateP1(year, month, day) = "Date 2011-07-15" // throws an exception at runtime

val copyright: String = dateP1 findFirstIn "Date of this document: 2011-07-15" match {
  case Some(dateP1(year, month, day)) => "Copyright "+year
  case None                           => "No copyright"
}

val copyright: Option[String] = for {
  dateP1(year, month, day) <- dateP1 findFirstIn "Last modified 2011-07-15"
} yield year

def getYears(text: String): Iterator[String] = for (dateP1(year, _, _) <- dateP1 findAllIn text) yield year
def getFirstDay(text: String): Option[String] = for (m <- dateP2 findFirstMatchIn text) yield m group "day"

Regex does not provide a method that returns a scala.Boolean. One can use java.lang.String matches method, or, if Regex is preferred, either ignore the return value or test the Option for emptyness. For example:

def hasDate(text: String): Boolean = (dateP1 findFirstIn text).nonEmpty
def printLinesWithDates(lines: Traversable[String]) {
  lines foreach { line =>
    dateP1 findFirstIn line foreach { _ => println(line) }
  }
}

There are also methods that can be used to replace the patterns on a text. The substitutions can be simple replacements, or more complex functions. For example:

val months = Map( 1 -> "Jan", 2 -> "Feb", 3 -> "Mar",
                  4 -> "Apr", 5 -> "May", 6 -> "Jun",
                  7 -> "Jul", 8 -> "Aug", 9 -> "Sep",
                  10 -> "Oct", 11 -> "Nov", 12 -> "Dec")

import scala.util.matching.Regex.Match
def reformatDate(text: String) = dateP2 replaceAllIn ( text, (m: Match) =>
  "%s %s, %s" format (months(m group "month" toInt), m group "day", m group "year")
)

You can use special pattern syntax constructs like (?idmsux-idmsux)¹ to switch various regex compilation options like CASE_INSENSITIVE or UNICODE_CASE.

Self Type
Regex
Annotations
@SerialVersionUID( uid = 2094783597747625537L )
Source
Regex.scala
Version

1.1, 29/01/2008

Note

¹ A detailed description is available in java.util.regex.Pattern.

See also

java.util.regex.Pattern

Linear Supertypes
Serializable, java.io.Serializable, AnyRef, Any
Known Subclasses
Type Hierarchy Learn more about scaladoc diagrams
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Regex
  2. Serializable
  3. Serializable
  4. AnyRef
  5. Any
Implicitly
  1. by any2stringadd
  2. by any2stringfmt
  3. by any2ArrowAssoc
  4. by any2Ensuring
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Instance Constructors

  1. new Regex(regex: String, groupNames: String*)

    regex

    A string representing a regular expression

    groupNames

    A mapping from names to indices in capture groups

Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Test two objects for inequality.

    Test two objects for inequality.

    returns

    true if !(this == that), false otherwise.

    Definition Classes
    Any
  3. final def ##(): Int

    Equivalent to x.hashCode except for boxed numeric types and null.

    Equivalent to x.hashCode except for boxed numeric types and null. For numerics, it returns a hash value which is consistent with value equality: if two value type instances compare as true, then ## will produce the same hash value for each of them. For null returns a hashcode where null.hashCode throws a NullPointerException.

    returns

    a hash value consistent with ==

    Definition Classes
    AnyRef → Any
  4. def +(other: String): String

    Implicit information
    This member is added by an implicit conversion from Regex to StringAdd performed by method any2stringadd in scala.Predef.
    Definition Classes
    StringAdd
  5. def ->[B](y: B): (Regex, B)

    Implicit information
    This member is added by an implicit conversion from Regex to ArrowAssoc[Regex] performed by method any2ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc
    Annotations
    @inline()
  6. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  7. final def ==(arg0: Any): Boolean

    Test two objects for equality.

    Test two objects for equality. The expression x == that is equivalent to if (x eq null) that eq null else x.equals(that).

    returns

    true if the receiver object is equivalent to the argument; false otherwise.

    Definition Classes
    Any
  8. def anchored: Regex

  9. final def asInstanceOf[T0]: T0

    Cast the receiver object to be of type T0.

    Cast the receiver object to be of type T0.

    Note that the success of a cast at runtime is modulo Scala's erasure semantics. Therefore the expression 1.asInstanceOf[String] will throw a ClassCastException at runtime, while the expression List(1).asInstanceOf[List[String]] will not. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the requested type.

    returns

    the receiver object.

    Definition Classes
    Any
    Exceptions thrown
    ClassCastException

    if the receiver object is not an instance of the erasure of type T0.

  10. def clone(): AnyRef

    Create a copy of the receiver object.

    Create a copy of the receiver object.

    The default implementation of the clone method is platform dependent.

    returns

    a copy of the receiver object.

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
    Note

    not specified by SLS as a member of AnyRef

  11. def ensuring(cond: (Regex) ⇒ Boolean, msg: ⇒ Any): Regex

    Implicit information
    This member is added by an implicit conversion from Regex to Ensuring[Regex] performed by method any2Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  12. def ensuring(cond: (Regex) ⇒ Boolean): Regex

    Implicit information
    This member is added by an implicit conversion from Regex to Ensuring[Regex] performed by method any2Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  13. def ensuring(cond: Boolean, msg: ⇒ Any): Regex

    Implicit information
    This member is added by an implicit conversion from Regex to Ensuring[Regex] performed by method any2Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  14. def ensuring(cond: Boolean): Regex

    Implicit information
    This member is added by an implicit conversion from Regex to Ensuring[Regex] performed by method any2Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  15. final def eq(arg0: AnyRef): Boolean

    Tests whether the argument (arg0) is a reference to the receiver object (this).

    Tests whether the argument (arg0) is a reference to the receiver object (this).

    The eq method implements an equivalence relation on non-null instances of AnyRef, and has three additional properties:

    • It is consistent: for any non-null instances x and y of type AnyRef, multiple invocations of x.eq(y) consistently returns true or consistently returns false.
    • For any non-null instance x of type AnyRef, x.eq(null) and null.eq(x) returns false.
    • null.eq(null) returns true.

    When overriding the equals or hashCode methods, it is important to ensure that their behavior is consistent with reference equality. Therefore, if two objects are references to each other (o1 eq o2), they should be equal to each other (o1 == o2) and they should hash to the same value (o1.hashCode == o2.hashCode).

    returns

    true if the argument is a reference to the receiver object; false otherwise.

    Definition Classes
    AnyRef
  16. def equals(arg0: Any): Boolean

    The equality method for reference types.

    The equality method for reference types. Default implementation delegates to eq.

    See also equals in scala.Any.

    returns

    true if the receiver object is equivalent to the argument; false otherwise.

    Definition Classes
    AnyRef → Any
  17. def finalize(): Unit

    Called by the garbage collector on the receiver object when there are no more references to the object.

    Called by the garbage collector on the receiver object when there are no more references to the object.

    The details of when and if the finalize method is invoked, as well as the interaction between finalize and non-local returns and exceptions, are all platform dependent.

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
    Note

    not specified by SLS as a member of AnyRef

  18. def findAllIn(source: CharSequence): MatchIterator

    Return all matches of this regexp in given character sequence as a scala.util.matching.Regex.MatchIterator, which is a special scala.collection.Iterator that returns the matched strings, but can also be converted into a normal iterator that returns objects of type scala.util.matching.Regex.Match that can be queried for data such as the text that precedes the match, subgroups, etc.

    Return all matches of this regexp in given character sequence as a scala.util.matching.Regex.MatchIterator, which is a special scala.collection.Iterator that returns the matched strings, but can also be converted into a normal iterator that returns objects of type scala.util.matching.Regex.Match that can be queried for data such as the text that precedes the match, subgroups, etc.

    source

    The text to match against.

    returns

    A scala.util.matching.Regex.MatchIterator of all matches.

    Example:
    1. for (words <- """\w+""".r findAllIn "A simple example.") yield words
  19. def findAllMatchIn(source: CharSequence): Iterator[Match]

    Return all matches of this regexp in given character sequence as a scala.collection.Iterator of scala.util.matching.Regex.Match.

    Return all matches of this regexp in given character sequence as a scala.collection.Iterator of scala.util.matching.Regex.Match.

    source

    The text to match against.

    returns

    A scala.collection.Iterator of scala.util.matching.Regex.Match for all matches.

    Example:
    1. for (words <- """\w+""".r findAllMatchIn "A simple example.") yield words.start
  20. def findFirstIn(source: CharSequence): Option[String]

    Return optionally first matching string of this regexp in given character sequence, or None if it does not exist.

    Return optionally first matching string of this regexp in given character sequence, or None if it does not exist.

    source

    The text to match against.

    returns

    An scala.Option of the first matching string in the text.

    Example:
    1. """\w+""".r findFirstIn "A simple example." foreach println // prints "A"
  21. def findFirstMatchIn(source: CharSequence): Option[Match]

    Return optionally first match of this regexp in given character sequence, or None if it does not exist.

    Return optionally first match of this regexp in given character sequence, or None if it does not exist.

    The main difference between this method and findFirstIn is that the (optional) return type for this is scala.util.matching.Regex.Match, through which more data can be obtained about the match, such as the strings that precede and follow it, or subgroups.

    source

    The text to match against.

    returns

    A scala.Option of scala.util.matching.Regex.Match of the first matching string in the text.

    Example:
    1. ("""[a-z]""".r findFirstMatchIn "A simple example.") map (_.start) // returns Some(2), the index of the first match in the text
  22. def findPrefixMatchOf(source: CharSequence): Option[Match]

    Return optionally match of this regexp at the beginning of the given character sequence, or None if regexp matches no prefix of the character sequence.

    Return optionally match of this regexp at the beginning of the given character sequence, or None if regexp matches no prefix of the character sequence.

    The main difference from this method to findFirstMatchIn is that this method will not return any matches that do not begin at the start of the text being matched against.

    source

    The text to match against.

    returns

    A scala.Option of the scala.util.matching.Regex.Match of the matched string.

    Example:
    1. """\w+""".r findPrefixMatchOf "A simple example." map (_.after) // returns Some(" simple example.")
  23. def findPrefixOf(source: CharSequence): Option[String]

    Return optionally match of this regexp at the beginning of the given character sequence, or None if regexp matches no prefix of the character sequence.

    Return optionally match of this regexp at the beginning of the given character sequence, or None if regexp matches no prefix of the character sequence.

    The main difference from this method to findFirstIn is that this method will not return any matches that do not begin at the start of the text being matched against.

    source

    The text to match against.

    returns

    A scala.Option of the matched prefix.

    Example:
    1. """[a-z]""".r findPrefixOf "A simple example." // returns None, since the text does not begin with a lowercase letter
  24. def formatted(fmtstr: String): String

    Returns string formatted according to given format string.

    Returns string formatted according to given format string. Format strings are as for String.format (@see java.lang.String.format).

    Implicit information
    This member is added by an implicit conversion from Regex to StringFormat performed by method any2stringfmt in scala.Predef.
    Definition Classes
    StringFormat
    Annotations
    @inline()
  25. final def getClass(): Class[_]

    A representation that corresponds to the dynamic class of the receiver object.

    A representation that corresponds to the dynamic class of the receiver object.

    The nature of the representation is platform dependent.

    returns

    a representation that corresponds to the dynamic class of the receiver object.

    Definition Classes
    AnyRef → Any
    Note

    not specified by SLS as a member of AnyRef

  26. def hashCode(): Int

    The hashCode method for reference types.

    The hashCode method for reference types. See hashCode in scala.Any.

    returns

    the hash code value for this object.

    Definition Classes
    AnyRef → Any
  27. final def isInstanceOf[T0]: Boolean

    Test whether the dynamic type of the receiver object is T0.

    Test whether the dynamic type of the receiver object is T0.

    Note that the result of the test is modulo Scala's erasure semantics. Therefore the expression 1.isInstanceOf[String] will return false, while the expression List(1).isInstanceOf[List[String]] will return true. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the specified type.

    returns

    true if the receiver object is an instance of erasure of type T0; false otherwise.

    Definition Classes
    Any
  28. final def ne(arg0: AnyRef): Boolean

    Equivalent to !(this eq that).

    Equivalent to !(this eq that).

    returns

    true if the argument is not a reference to the receiver object; false otherwise.

    Definition Classes
    AnyRef
  29. final def notify(): Unit

    Wakes up a single thread that is waiting on the receiver object's monitor.

    Wakes up a single thread that is waiting on the receiver object's monitor.

    Definition Classes
    AnyRef
    Note

    not specified by SLS as a member of AnyRef

  30. final def notifyAll(): Unit

    Wakes up all threads that are waiting on the receiver object's monitor.

    Wakes up all threads that are waiting on the receiver object's monitor.

    Definition Classes
    AnyRef
    Note

    not specified by SLS as a member of AnyRef

  31. val pattern: Pattern

    The compiled pattern

  32. def replaceAllIn(target: CharSequence, replacer: (Match) ⇒ String): String

    Replaces all matches using a replacer function.

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

    import scala.util.matching.Regex
    val 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 => 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. One can use scala.util.matching.Regex's quoteReplacement to automatically escape these characters.

    target

    The string to match.

    replacer

    The function which maps a match to another string.

    returns

    The target string after replacements.

  33. def replaceAllIn(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. One can use scala.util.matching.Regex's quoteReplacement to automatically escape these characters.

    target

    The string to match

    replacement

    The string that will replace each match

    returns

    The resulting string

    Example:
    1. """\d+""".r replaceAllIn ("July 15", "") // returns "July "
  34. def replaceFirstIn(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. One can use scala.util.matching.Regex's quoteReplacement to automatically escape these characters.

    target

    The string to match

    replacement

    The string that will replace the match

    returns

    The resulting string

  35. def replaceSomeIn(target: CharSequence, replacer: (Match) ⇒ Option[String]): String

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

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

    import scala.util.matching.Regex._
    
    val map = Map("x" -> "a var", "y" -> """some $ and \ signs""")
    val text = "A text with variables %x, %y and %z."
    val varPattern = """%(\w+)""".r
    val mapper = (m: Match) => map 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. One can use scala.util.matching.Regex's quoteReplacement to automatically escape these characters.

    target

    The string to match.

    replacer

    The function which optionally maps a match to another string.

    returns

    The target string after replacements.

  36. def runMatcher(m: Matcher): Boolean

    Attributes
    protected
  37. def split(toSplit: CharSequence): Array[String]

    Splits the provided character sequence around matches of this regexp.

    Splits the provided character sequence around matches of this regexp.

    toSplit

    The character sequence to split

    returns

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

  38. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  39. def toString(): String

    The string defining the regular expression

    The string defining the regular expression

    returns

    a String representation of the object.

    Definition Classes
    Regex → AnyRef → Any
  40. def unanchored: UnanchoredRegex

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

    Create a new Regex with the same pattern, but no requirement that the entire String matches in extractor patterns. For instance, the strings shown below lead to successful matches, where they would not otherwise.

    val dateP1 = """(\d\d\d\d)-(\d\d)-(\d\d)""".r.unanchored
    
    val dateP1(year, month, day) = "Date 2011-07-15"
    
    val copyright: String = "Date of this document: 2011-07-15" match {
      case dateP1(year, month, day) => "Copyright "+year
      case _                        => "No copyright"
    }
    returns

    The new unanchored regex

  41. def unapplySeq(target: Any): Option[List[String]]

    Tries to match target (whole match) and returns the matching subgroups.

    Tries to match target (whole match) and returns the matching subgroups. if the pattern has no subgroups, then it returns an empty list on a successful match.

    Note, however, that if some subgroup has not been matched, a null will be returned for that subgroup.

    For example:

    val p1 = "ab*c".r
    val p2 = "a(b*)c".r
    
    val p1Matches = "abbbc" match {
      case p1() => true
      case _    => false
    }
    
    val numberOfB = "abbbc" match {
      case p2(b) => Some(b.length)
      case _     => None
    }
    target

    The string to match

    returns

    The matches

  42. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  43. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  44. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  45. def [B](y: B): (Regex, B)

    Implicit information
    This member is added by an implicit conversion from Regex to ArrowAssoc[Regex] performed by method any2ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc

Shadowed Implicit Value Members

  1. val self: Any

    Implicit information
    This member is added by an implicit conversion from Regex to StringAdd performed by method any2stringadd in scala.Predef.
    Shadowing
    This implicitly inherited member is ambiguous. One or more implicitly inherited members have similar signatures, so calling this member may produce an ambiguous implicit conversion compiler error.
    To access this member you can use a type ascription:
    (regex: StringAdd).self
    Definition Classes
    StringAdd
  2. val self: Any

    Implicit information
    This member is added by an implicit conversion from Regex to StringFormat performed by method any2stringfmt in scala.Predef.
    Shadowing
    This implicitly inherited member is ambiguous. One or more implicitly inherited members have similar signatures, so calling this member may produce an ambiguous implicit conversion compiler error.
    To access this member you can use a type ascription:
    (regex: StringFormat).self
    Definition Classes
    StringFormat

Deprecated Value Members

  1. def x: Regex

    Implicit information
    This member is added by an implicit conversion from Regex to ArrowAssoc[Regex] performed by method any2ArrowAssoc in scala.Predef.
    Shadowing
    This implicitly inherited member is ambiguous. One or more implicitly inherited members have similar signatures, so calling this member may produce an ambiguous implicit conversion compiler error.
    To access this member you can use a type ascription:
    (regex: ArrowAssoc[Regex]).x
    Definition Classes
    ArrowAssoc
    Annotations
    @deprecated
    Deprecated

    (Since version 2.10.0) Use leftOfArrow instead

  2. def x: Regex

    Implicit information
    This member is added by an implicit conversion from Regex to Ensuring[Regex] performed by method any2Ensuring in scala.Predef.
    Shadowing
    This implicitly inherited member is ambiguous. One or more implicitly inherited members have similar signatures, so calling this member may produce an ambiguous implicit conversion compiler error.
    To access this member you can use a type ascription:
    (regex: Ensuring[Regex]).x
    Definition Classes
    Ensuring
    Annotations
    @deprecated
    Deprecated

    (Since version 2.10.0) Use resultOfEnsuring instead

Inherited from Serializable

Inherited from java.io.Serializable

Inherited from AnyRef

Inherited from Any

Inherited by implicit conversion any2stringadd from Regex to StringAdd

Inherited by implicit conversion any2stringfmt from Regex to StringFormat

Inherited by implicit conversion any2ArrowAssoc from Regex to ArrowAssoc[Regex]

Inherited by implicit conversion any2Ensuring from Regex to Ensuring[Regex]

Ungrouped