Scala Regular Expressions
Scala supports regular expressions through the Regex class in the scala.util.matching package. The following example demonstrates using a regular expression to find the word Scala :
Example
import
scala.
util
.
matching
.
Regex
object
Test
{
def
main
(
args
:
Array
[
String
]
)
{
val
pattern
=
"Scala"
.
r
val
str
=
"Scala is Scalable and cool"
println
(
pattern findFirstIn str
)
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
Some(Scala)
The example uses the r() method of the String class to construct a Regex object.
Then the findFirstIn method is used to find the first match.
If you need to see all matches, you can use the findAllIn method.
You can use the mkString( ) method to concatenate the strings matched by a regular expression, and use the pipe (|) to set different patterns:
Example
import
scala.
util
.
matching
.
Regex
object
Test
{
def
main
(
args
:
Array
[
String
]
)
{
val
pattern
=
new
Regex
(
"(S|s)cala"
)
// the first letter can be uppercase S or lowercase s
val
str
=
"Scala is scalable and cool"
println
(
(
pattern findAllIn str
)
.
mkString
(
","
)
)
// use a comma , to join the returned results
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
Scala,scala
If you need to replace the matched text with a specified keyword, you can use the replaceFirstIn( ) method to replace the first match, and the replaceAllIn( ) method to replace all matches, as shown in the example below:
Example
object
Test
{
def
main
(
args
:
Array
[
String
]
)
{
val
pattern
=
"(S|s)cala"
.
r
val
str
=
"Scala is scalable and cool"
println
(
pattern replaceFirstIn
(
str,
"Java"
)
)
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
Java is scalable and cool
Regular expressions
Scala's regular expressions inherit Java's syntax rules, which mostly use Perl's rules.
The table below gives some commonly used regular expression rules:
| Expression | Matching Rules |
|---|---|
| ^ | Matches the position at the start of the input string. |
| $ | Matches the position at the end of the input string. |
| . | Matches any single character except "\\r\\n". |
| [...] | Character set. Matches any one of the contained characters. For example, "[abc]" matches "a" in "plain". |
| [^...] | Negated character set. Matches any character not contained. For example, "[^abc]" matches "p", "l", "i", "n" in "plain". |
| \A | Matches the position at the start of the input string (no multiline support) |
| \z | End of string (similar to $, but not affected by the multiline option) |
| \Z | End of string or line (not affected by the multiline option) |
| re* | Repeats zero or more times |
| re+ | Repeats one or more times |
| re? | Repeats zero or one time |
| re{ n} | Repeats n times |
| re{ n,} | |
| re{ n, m} | Repeats n to m times |
| a|b | Matches a or b |
| (re) | Matches re and captures the text into an automatically named group |
| (?: re) | Matches re without capturing the matched text or assigning a group number |
| (?> re) | Greedy subexpression |
| \w | Matches a letter, digit, or underscore |
| \W | Matches any character that is not a letter, digit, underscore, or Chinese character |
| \s | Matches any whitespace character, equivalent to [\\t\\n\\r\\f] |
| \S | Matches any character that is not whitespace |
| \d | Matches a digit, similar to [0-9] |
| \D | Matches any non-digit character |
| \G | The start of the current search |
| \n | Newline |
| \b | Usually a word-boundary position, but inside a character class it represents backspace |
| \B | Matches a position that is not the start or end of a word |
| \t | Tab |
| \Q | Start quote: **\\Q(a+b)3\E* can match the text "(a+b)*3". |
| \E | End quote: **\\Q(a+b)3\E* can match the text "(a+b)*3". |
Regular expression example
| Example | Description |
|---|---|
| . | Matches any single character except "\\r\\n". |
| [Rr]uby | Matches "Ruby" or "ruby" |
| rub[ye] | Match "ruby" or "rube" |
| [aeiou] | Matches lowercase letters: aeiou |
| [0-9] | Matches any digit, similar to [0123456789] |
| [a-z] | Matches any ASCII lowercase letter |
| [A-Z] | Matches any ASCII uppercase letter |
| [a-zA-Z0-9] | Matches digits and upper/lowercase letters |
| [^aeiou] | Matches any character except aeiou |
| [^0-9] | Matches any character except digits |
| \d | Matches a digit, similar to: [0-9] |
| \D | Matches a non-digit, similar to: [^0-9] |
| \s | Matches whitespace, similar to: [ \\t\\r\\n\\f] |
| \S | Matches non-whitespace, similar to: [^ \\t\\r\\n\\f] |
| \w | Matches a letter, digit, or underscore, similar to: [A-Za-z0-9_] |
| \W | Matches anything that is not a letter, digit, or underscore, similar to: [^A-Za-z0-9_] |
| ruby? | Matches "rub" or "ruby": y is optional |
| ruby* | Matches "rub" plus zero or more y's. |
| ruby+ | Matches "rub" plus one or more y's. |
| \d{3} | Matches exactly 3 digits. |
| \d{3,} | Matches 3 or more digits. |
| \d{3,5} | Matches 3, 4, or 5 digits. |
| \D\d+ | No grouping: + repeats \\d |
| (\D\d)+/ | Grouping: + repeats \\D\\d pairs |
| ([Rr]uby(, )?)+ | Matches "Ruby", "Ruby, ruby, ruby", and so on |
Note that each character in the table above uses two backslashes. This is because a backslash in a string is an escape character in Java and Scala. So if you want to output a backslash, you need to write two backslashes in a string. See the example below:
Example
import
scala.
util
.
matching
.
Regex
object
Test
{
def
main
(
args
:
Array
[
String
]
)
{
val
pattern
=
new
Regex
(
"abl[ae]\d+"
)
val
str
=
"ablaw is able1 and cool"
println
(
(
pattern findAllIn str
)
.
mkString
(
","
)
)
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
able1