scala 正则表达式_Scala正则表达式示例

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() method.

在以上示例中,我们找到了“功能性”一词。 我们调用r()方法,该方法将字符串转换为RichString并调用Regex实例。 findFirstIn方法查找模式的首次出现。 要查找所有出现的事件,请使用finadAllIn()方法。

If there is a match, scala returns an object. To return the actual string, we use mkString.

如果存在匹配项,scala将返回一个对象。 要返回实际的字符串,我们使用mkString

The mkString method concatenates the resulting set. Pipe (|) symbol can be used to specify the OR search condition. For example, small and capital case of the letter ‘S’ in the word ‘Scala’. Instead of using r() constructor the Regex constructor can be used.

mkString方法连接结果集。 竖线(|)符号可用于指定OR搜索条件。 例如,单词“ Scala”中字母“ S”的小写大写字母。 代替使用r()构造函数,可以使用Regex构造函数。

Consider an example using regex constructor;

考虑一个使用正则表达式构造函数的示例;

import scala.util.matching.Regex

object multipleoccurence {
   def main(args: Array[String]) {
      val p = new Regex("(S|s)tudent")
      val st = "Student Id is unique. Students are interested in learning new things"
      
      println((p findAllIn st).mkString(","))
   }
}

Above main method will produce output as;

以上主要方法将产生输出为;

Student,Student

The replaceFirstIn( ) can be used to replace the first occurrence of the matching word and replaceAllIn( ) replaces all the occurrences.

replaceFirstIn( )可用于替换匹配单词的第一个匹配项,而replaceAllIn( )替换所有匹配的单词。

Consider an example below.

考虑下面的示例。

object Replace {
   def main(args: Array[String]) {
  	val p = "Car".r
  	val st = "Car has power windows"
 	 
  	println(p replaceFirstIn(st, "Alto"))
   }
}

Output:

输出:

Alto has power windows

Here the word Car is replaced by Alto using replaceFirstIn method.

在这里,单词Car被Alto使用replaceFirstIn方法替换。

形成正则表达式 (Forming Regular Expressions)

The following regular expression operators are supported in Scala.

Scala支持以下正则表达式运算符。

. – Matches any single character except newline

。 –匹配除换行符以外的任何单个字符

$ – Matches end of line

$ –匹配行尾

^ – Matches beginning of line

^ –匹配行首

[…] – Matches any single character in brackets

[…] –匹配方括号中的任何单个字符

[^…] – Matches any single character excluding the characters in brackets

[^…] –匹配除括号中的字符外的任何单个字符

\\A – Matches the string beginning with A

\\ A –匹配以A开头的字符串

re* – Matches 0 or more occurrences of preceding expression

re * –匹配0次或多次出现的前一个表达式

re+ – Matches 1 or more of the previous thing

re + –匹配上一项或多项

re? – Matches 0 or 1 occurrence of preceding expression

回覆? –匹配前面的表达式的0或1

re{n} – Matches exactly n number of occurrences of preceding expression

re {n} –精确匹配前一个表达式的n次出现

re{n,} – Matches n or more occurrences of preceding expression

re {n,} –匹配n次或多次出现的前一个表达式

re{n,m} – Matches at least n and at most m occurrences of preceding expression

re {n,m} –至少匹配n个,最多匹配m个前面的表达式

x|y – Matches either x or y

x | y –匹配x或y

(re) – Groups regular expressions and remembers matched text

(重新)–对正则表达式进行分组并记住匹配的文本

(?: re) – Groups regular expressions without remembering matched text

(?:re)–对正则表达式进行分组而不记住匹配的文本

(?> re) – Matches independent pattern without backtracking

(?> re)–匹配独立模式而不回溯

\\w – Matches word characters

\\ w –匹配单词字符

\\W – Matches nonword characters

\\ W –匹配非单词字符

\\s – Matches whitespace. Equivalent to [\t\n\r\f]

\\ s –匹配空格。 等效于[\ t \ n \ r \ f]

\\S – Matches nonwhitespace

\\ S –匹配非空格

\\d – Matches digits. Equivalent to [0-9]

\\ d –匹配数字。 相当于[0-9]

\\D – Matches nondigits.

\\ D –匹配非数字。

\\A – Matches beginning of string

\\ A –匹配字符串的开头

\\Z – Matches end of string. If a newline exists, it matches just before newline

\\ Z –匹配字符串的结尾。 如果存在换行符,则匹配换行符

\\z – Matches end of string

\\ z –匹配字符串的结尾

\\G – Matches point where last match finished

\\ G –上一次比赛结束的比赛点

\\n – Back-reference to capture group number “n”

\\ n –反向引用捕获组号“ n”

\\b – Matches word boundaries when outside brackets

\\ b –在方括号外时匹配单词边界

\\B- Matches nonword boundaries

\\ B-匹配非单词边界

\\n, \\t, etc.- Matches newlines, carriage returns, tabs, etc.

\\ n,\\ t等-匹配换行符,回车符,制表符等。

\\Q – Escape (quote) all characters up to \\E

\\ Q –转义(引用)所有字符,直到\\ E

\\E – Ends quoting begun with \\Q

\\ E –以\\ Q开头的结尾报价

Consider an example which matches all the occurrence of the pattern in the statement.

考虑一个匹配语句中所有出现模式的示例。

import scala.util.matching.Regex

object findAll {
   def main(args: Array[String]) {
  	val p = new Regex("al+")
  	val st = "Scala is a Functional programming language"
 	 
  	println((p findAllIn st).mkString(","))
   }
}

Output:

输出:

al,al

That’s all for a quick roundup on Scala Regular Expression, we will look into Scala Extractors in next article.

这就是对Scala正则表达式的快速汇总,我们将在下一篇文章中研究Scala提取器。

翻译自: https://www.journaldev.com/8256/scala-regular-expression-example

scala 正则表达式

你可能感兴趣的:(字符串,正则表达式,java,python,regex)