scala 正则表达式
Regular expressions are pattern matching utilities found in most of the programming languages. They define a generic pattern to match a sequence of input characters. Regex are widely used in text parsing and search.
正则表达式是大多数编程语言中都可以找到的模式匹配实用程序。 它们定义了通用模式来匹配一系列输入字符。 正则表达式广泛用于文本解析和搜索。
The Regex class in scala is available in scala.util.matching package.
scala中的Regex类可在scala.util.matching包中找到。
Consider an example of how to find a word below.
考虑下面如何查找单词的示例。
import scala.util.matching.Regex
object findWord {
def main(args: Array[String]) {
val p = "Functional".r
val st = "Scala is a Functional Programming Language"
println(p findFirstIn st)
}
}
Below image shows the output produced when we execute this object main method.
下图显示了执行此对象main方法时产生的输出。
In the above example we are finding the word “functional” . We invoke the r() method which converts string to RichString and invokes the instance of Regex. The findFirstIn
method finds the first occurrence of the pattern. To find all the occurrences use finadAllIn()</