Return all matches of this regexp in given character sequence as a scala.util.mathcing.Regex.MatchIterator, which is a special Iterator that returns the matched strings, but can also be converted into a normal iterator that returns objects of type 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.mathcing.Regex.MatchIterator, which is a special Iterator that returns the matched strings, but can also be converted into a normal iterator that returns objects of type Match that can be queried for data such as the text that precedes the match, subgroups, etc.
The text to match against.
A scala.util.matching.Reegex.MatchIterator of all matches.
for (words <- """\w+""".r findAllIn "A simple example.") yield words
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.
The text to match against.
An Option of the first matching string in the text.
"""\w+""".r findFirstIn "A simple example." foreach println // prints "A"
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 Match, through which more
data can be obtained about the match, such as the strings that precede and follow it,
or subgroups.
("""[a-z]""".r findFirstMatchIn "A simple example.") map (_.start) // returns Some(2), the index of the first match in the text
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.
"""\w+""".r findPrefixMatchOf "A simple example." map (_.after) // returns Some(" simple example.")
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.
The text to match against.
A Option of the matched prefix.
"""[a-z]""".r findPrefixOf "A simple example." // returns None, since the text does not begin with a lowercase letter
The compiled pattern
Replaces all matches using a replacer function.
Replaces all matches using a replacer function. The replacer function takes a 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 Regex's quoteReplacement
to automatically escape these characters.
The string to match.
The function which maps a match to another string.
The target string after replacements.
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 Regex's quoteReplacement
to automatically escape these characters.
The string to match
The string that will replace each match
The resulting string
"""\d+""".r replaceAllIn ("July 15", "" ) // returns "July"
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 Regex's quoteReplacement
to automatically escape these characters.
The string to match
The string that will replace the match
The resulting string
Replaces some of the matches using a replacer function that returns an Option.
Replaces some of the matches using a replacer function that returns an Option. The replacer function takes a 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 Regex's quoteReplacement
to automatically escape these characters.
The string to match.
The function which optionally maps a match to another string.
The target string after replacements.
Splits the provided character sequence around matches of this regexp.
Splits the provided character sequence around matches of this regexp.
The character sequence to split
The array of strings computed by splitting the input around matches of this regexp
The string defining the regular expression
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 }
The string to match
The matches
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 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.
There are two ways of using a
Regex
to find a pattern: calling methods on Regex, such asfindFirstIn
orfindAllIn
, 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:
Regex does not provide a method that returns a Boolean. One can use java.lang.String
matches
method, or, ifRegex
is preferred, either ignore the return value or test theOption
for emptyness. For example: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:
You can use special pattern syntax constructs like
(?idmsux-idmsux)
¹ to switch various regex compilation options likeCASE_INSENSITIVE
orUNICODE_CASE
.1.1, 29/01/2008
¹ A detailed description is available in java.util.regex.Pattern.
java.util.regex.Pattern