Scala passed Execute the above code, and the output is as follows: Use in the instance And then use the If you need to see all matches, you can use the You can use it. Execute the above code, and the output is as follows: If you need to replace the matching text with the specified keywords, you can use the Execute the above code, and the output is as follows: Scala’s regular expressions inherit the syntax rules of Java, while Java mostly uses the rules of the Perl language. In the following table, we give some common regular expression rules: Expression. Matching rule Matches the position where the input string begins. Matches the position at the end of the input string. Matches any single character except “rn”. Character set. Matches any character contained. For example, “ [abc] Match the “a” in “plain”. Reverse character set. Matches any characters that are not included. For example, “ [^abc] Match “p”, “l”, “I”, “n” in “plain”. Match the position where the input string begins (no multiline support) End of string (similar to $, but not affected by handling multiline options) End of string or end of line (not affected by handling multiline options) Repeat zero or more times Repeat one or more times Repeat zero or once Repeat n times Repeat n to m times Match an or b Match the re and capture the text to the automatically named group Match re, no matching text is captured, and no group number is assigned to this packet Greedy subexpression Match letters or numbers or underscores Match any character that is not letters, numbers, underscores, or Chinese characters Match any whitespace character, equal to [tnrf] Match any character that is not a blank character Match numbers, similar [0-9] Match any non-numeric character The beginning of the current search Newline character It is usually the word demarcation position, but if you use it in a character class to represent backspace Matching is not the beginning or end of a word. Tab character Opening quotation marks:Q (aqb) 3E? Matchable Text “(a+b) 3 “. Closing quotation marks:Q (aqb) 3E? Matchable Text “(a+b) 3 “. Regular expression instance Example Description Matches any single character except “rn”. Match “Ruby” or “ruby” Match “ruby” or “rube” Match lowercase letters: aeiou Match any number, similar to [0123456789] Match any ASCII lowercase letter Match any ASCII capital letters Match numbers, upper and lowercase letters Match other characters except aeiou Match characters other than numbers Match numbers, similar to: [0-9] Match non-numeric, similar to: Match spaces, similar to: Match spaces, similar to: Match letters, numbers, underscores, similar to: Match non-letters, numbers, underscores, similar to: Matching “rub” or “ruby”: y is optional Matches “rub” plus 0 or more y. Matches “rub” plus one or more y. It matches exactly three numbers. Match 3 or more digits. Match 3, 4, or 5 digits. No grouping: + repeat Grouping: + repeat Match “Ruby”, “Ruby, ruby, ruby”, etc. Note that each character in the table above uses two backslashes. This is because backslashes in strings in Java and Scala are escape characters. Therefore, if you want to output Execute the above code, and the output is as follows:
scala.util.matching
in the bag
Regex
class to supportregular expressions. The following example demonstrates using regular expressions to find words
Scala
: 8.39.1. 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)
}
}
$ scalac Test.scala
$ scala Test
Some(Scala)
String
analogous
r()
method constructs a Regexobject.
findFirstIn
method to find the first match.
findAllIn
method.
mkString(
)
method to concatenate strings of regular expression matching results, and you can use pipes (|) to set different modes: 8.39.2. Example #
import scala.util.matching.Regex
object Test {
def main(args: Array[String]) {
val pattern = new Regex("(S|s)cala") // The initial letter can be uppercase S or lowercase
s
val str = "Scala is scalable and cool"
println((pattern findAllIn str).mkString(",")) // Using commas ,
Connection return result
}
}
$ scalac Test.scala
$ scala Test
Scala,scala
replaceFirstIn(
)
method to replace the first match, using the
replaceAllIn(
)
method to replace all matches, as an example: 8.39.3. 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"))
}
}
$ scalac Test.scala
$ scala Test
Java is scalable and cool
Regular expression #
^
$
.
[...]
[^...]
\\A
\\z
\\Z
re*
re+
re?
re{
n}
re{
n,}
re{
n,
m}
a|b
(re)
(?:
re)
(?>
re)
\\w
\\W
\\s
\\S
\\d
\\D
\\G
\\n
\\b
\\B
\\t
\\Q
\\E
.
[Rr]uby
rub[ye]
[aeiou]
[0-9]
[a-z]
[A-Z]
[a-zA-Z0-9]
[^aeiou]
[^0-9]
\\d
\\D
[^0-9]
\\s
[
\t\r\n\f]
\\S
[^
\t\r\n\f]
\\w
[A-Za-z0-9_]
\\W
[^A-Za-z0-9_]
ruby?
ruby*
ruby+
\\d{3}
\\d{3,}
\\d{3,5}
\\D\\d+
\d
(\\D\\d)+/
\D\d
Yes
([Rr]uby(,
)?)+
\\
, you need to write it as
\\
in the string to obtain a backslash. See the following example: 8.39.4. 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(","))
}
}
$ scalac Test.scala
$ scala Test
able1