博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
scala 正则表达式_Scala正则表达式示例
阅读量:2532 次
发布时间:2019-05-11

本文共 5377 字,大约阅读时间需要 17 分钟。

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.Regexobject 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.Regexobject 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.Regexobject 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提取器。

翻译自:

scala 正则表达式

转载地址:http://cvlzd.baihongyu.com/

你可能感兴趣的文章
安装php扩展
查看>>
百度移动搜索主要有如下几类结果构成
查看>>
Python爬虫面试题170道:2019版【1】
查看>>
JavaBean规范
查看>>
第四阶段 15_Linux tomcat安装与配置
查看>>
NAS 创建大文件
查看>>
学习笔记-模块之xml文件处理
查看>>
接口测试用例
查看>>
面试:用 Java 实现一个 Singleton 模式
查看>>
Sybase IQ导出文件的几种方式
查看>>
案例:手动输入一个字符串,打散放进一个列表,小写字母反序 大写字母保持不变...
查看>>
linux 系统下 tar 的压缩与解压缩命令
查看>>
阿里负载均衡,配置中间证书问题(在starcom申请免费DV ssl)
查看>>
转:How to force a wordbreaker to be used in Sharepoint Search
查看>>
MySQL存储过程定时任务
查看>>
Python中and(逻辑与)计算法则
查看>>
POJ 3267 The Cow Lexicon(动态规划)
查看>>
设计原理+设计模式
查看>>
音视频处理
查看>>
tomcat 7服务器跨域问题解决
查看>>