MappableRegex

com.phasmidsoftware.xmldoc.core.MappableRegex
class MappableRegex(r: String, f: List[String] => List[String]) extends Regex

A class that extends the standard Regex functionality by allowing a transformation to be applied to the sequence of matched groups. This is achieved by providing a function that maps a list of matched groups to a new transformed list.

Value parameters

f

A function that takes a list of matched groups and transforms it into another list.

r

The regular expression pattern as a string.

Attributes

Constructor

Creates a MappableRegex with a given regular expression pattern and a transformation function.

Graph
Supertypes
class Regex
trait Serializable
class Object
trait Matchable
class Any
Known subtypes

Members list

Value members

Concrete methods

override def unapplySeq(s: CharSequence): Option[List[String]]

Extracts matched groups from the provided character sequence and applies a transformation function to the list of matched groups.

Extracts matched groups from the provided character sequence and applies a transformation function to the list of matched groups.

Value parameters

s

The character sequence to match against the regular expression.

Attributes

Returns

An Option containing a transformed list of matched groups if the input matches the regular expression, or None if there is no match.

Definition Classes
Regex

Inherited methods

def anchored: Regex

Attributes

Inherited from:
Regex
def findAllIn(source: CharSequence): MatchIterator

Returns all non-overlapping matches of this Regex in the 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 queried for more data about the last match, such as capturing groups and start position.

Returns all non-overlapping matches of this Regex in the 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 queried for more data about the last match, such as capturing groups and start position.

A MatchIterator can also be converted into an iterator that returns objects of type scala.util.matching.Regex.Match, such as is normally returned by findAllMatchIn.

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]+".r
val 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]+))".r
val hathaway = "hathatthattthatttt"
val 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 in java.lang.IllegalStateException. See scala.util.matching.Regex.MatchIterator for details.

Value parameters

source

The text to match against.

Attributes

Returns

A scala.util.matching.Regex.MatchIterator of matched substrings.

Example

Inherited from:
Regex
def findAllMatchIn(source: CharSequence): Iterator[Match]

Returns all non-overlapping matches of this regexp in given character sequence as a scala.collection.Iterator of scala.util.matching.Regex.Match.

Returns all non-overlapping matches of this regexp in given character sequence as a scala.collection.Iterator of scala.util.matching.Regex.Match.

Value parameters

source

The text to match against.

Attributes

Returns

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

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

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

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

Value parameters

source

The text to match against.

Attributes

Returns

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

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

Returns an optional first match of this Regex in the given character sequence, or None if it does not exist.

Returns an optional first match of this Regex in the given character sequence, or None if it does not exist.

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

Value parameters

source

The text to match against.

Attributes

Returns

A scala.Option of scala.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
Inherited from:
Regex
def findPrefixMatchOf(source: CharSequence): Option[Match]

Returns an optional match of this Regex at the beginning of the given character sequence, or None if it matches no prefix of the character sequence.

Returns an optional match of this Regex at the beginning of the given character sequence, or None if it matches no prefix of the character sequence.

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

Value parameters

source

The text to match against.

Attributes

Returns

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

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

Returns an optional match of this Regex at the beginning of the given character sequence, or None if it matches no prefix of the character sequence.

Returns an optional match of this Regex at the beginning of the given character sequence, or None if it matches no prefix of the character sequence.

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

Value parameters

source

The text to match against.

Attributes

Returns

A scala.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
Inherited from:
Regex
def matches(source: CharSequence): Boolean

Returns whether this Regex matches the given character sequence.

Returns whether this Regex 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 if source matches this Regex.

See also

Regex#unanchored

Example
"""\d+""".r matches "123" // returns true
Inherited from:
Regex
def regex: String

Attributes

Inherited from:
Regex
def replaceAllIn(target: CharSequence, replacer: Match => String): String

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:

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 => 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. Use Regex.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.

Inherited from:
Regex
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. Use Regex.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>"
Inherited from:
Regex
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. Use Regex.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

Inherited from:
Regex
def replaceSomeIn(target: CharSequence, replacer: Match => Option[String]): String

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 obtained from the match. For example:

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 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+)""".r
val 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. Use Regex.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.

Inherited from:
Regex
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.

Value parameters

toSplit

The character sequence to split

Attributes

Returns

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

Inherited from:
Regex
override def toString(): String

The string defining the regular expression.

The string defining the regular expression.

Attributes

Definition Classes
Regex -> Any
Inherited from:
Regex
def unanchored: UnanchoredRegex

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

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

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

The unanchored Regex behaves as though those anchors were removed.

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

Calling anchored returns the original Regex.

val date = """(\d\d\d\d)-(\d\d)-(\d\d)""".r.unanchored

val date(year, month, day) = "Date 2011-07-15"                       // OK

val 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

Inherited from:
Regex
def unapplySeq(m: Match): Option[List[String | Null]]

Tries to match on a scala.util.matching.Regex.Match.

Tries to match on a scala.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.

Value parameters

m

the Match to extract groups from

Attributes

Returns

the matched groups, or None if the match was unsuccessful

Inherited from:
Regex
def unapplySeq(c: Char): Option[List[Char]]

Tries to match the String representation of a scala.Char.

Tries to match the String representation of a scala.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 match
val r = """(\p{Lower})""".r
cat(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 extract
val r2 = """\p{Lower}""".r
cat(0) match { case r2(x) => true }    // no match
cat(0) match { case r2(_) => true }    // no match
cat(0) match { case r2(_*) => true }   // matches
cat(0) match { case r2() => true }     // matches

// even if there are multiple groups, only one is returned
val r3 = """((.))""".r
cat(0) match { case r3(_) => true }    // matches
cat(0) match { case r3(_,_) => true }  // no match

Value parameters

c

The Char to match

Attributes

Returns

The match

Inherited from:
Regex

Inherited fields

val pattern: Pattern

Attributes

Inherited from:
Regex