[Scala]Scala学习笔记七 正则表达式
2024-10-19 03:32:07
1. Regex对象
我们可以使用scala.util.matching.Regex
类使用正则表达式.要构造一个Regex对象,使用String类的r方法即可:
val numPattern = "[0-9]+".r
如果正则表达式包含反斜杠或引号的话,那么最好使用"原始"字符串语法"""..."""
:
val positiveNumPattern = """^[1-9]\d*$"""
如果在Java中使用上述正则表达式,则应该使用下面方式(需要进行转义):
val positiveNumPattern = "^[1-9]\\d*$"
相对于在Java中的使用方式,Scala这种写法可能更易读一些.
2. findAllIn
findAllIn方法返回遍历所有匹配项的迭代器.可以在for循环中使用它:
val str = "a b 27 c 6 d 1"
val numPattern = "[0-9]+".r
for(matchingStr <- numPattern.findAllIn(str)){
println(matchingStr)
}
或者将迭代器转成数组:
val str = "a b 27 c 6 d 1"
val numPattern = "[0-9]+".r
val matches = numPattern.findAllIn(str).toArray
// Array(27,6,1)
3. findPrefixOf
检查某个字符串的前缀是否能匹配,可以使用findPrefixOf方法:
val str = "3 a b 27 c 6 d 1"
val str2 = "a b 27 c 6 d 1"
val numPattern = "[0-9]+".r
val matches = numPattern.findPrefixOf(str)
val matches2 = numPattern.findPrefixOf(str2)
println(matches) // Some(3)
println(matches2) // None
4. replaceFirstIn replaceAllIn
可以使用如下命令替换第一个匹配项或者替换全部匹配项:
val str = "3 a b 27 c 6 d 1"
val numPattern = "[0-9]+".r
val matches = numPattern.replaceFirstIn(str, "*")
val matches2 = numPattern.replaceAllIn(str, "*")
println(matches) // * a b 27 c 6 d 1
println(matches2) // * a b * c * d *
5. 正则表达式组
分组可以让我们方便的获取正则表达式的子表达式.在你想要提取的子表达式两侧加上圆括号:
val str = "3 a"
val numPattern = "([0-9]+) ([a-z]+)".r
val numPattern(num, letter) = str
println(num) // 3
println(letter) // a
上述代码将num设置为3,letter设置为a
如果想从多个匹配项中提取分组内容,可以使用如下命令:
val str = "3 a b c 4 f"
val numPattern = "([0-9]+) ([a-z]+)".r
for(numPattern(num, letter) <- numPattern.findAllIn(str)){
println(num + "---"+letter)
}
// 3---a
// 4---f
最新文章
- SVN cleanup操作反复失败解决办法
- 深入学习golang(5)—接口
- Redis内存存储结构分析
- DBGridEh 点击表头排序方法
- url中的scheme
- 各种";居中";
- Hiberante+jpa,注解生成32字符的Id
- HTML5每日一练之input新增加的六种时间类型应用
- ssm框架查询数据并实现分页功能示例
- Java中Queue类实现
- How to download a website for offline usage
- Check Mysql Database Size
- The test form is only available for requests from the local machine 解决方法
- Verilog中的标点
- Use Node.js DDP Client on Arduino Yun to Access Meteor Server
- Wakelock API详解
- JSP中动态include和静态include区别
- hbuilder vs2013api Access-Control-Allow-Origin
- git取消跟踪已版本控制的文件
- loadrunner中使用web_custom_request函数调用webservice接口
热门文章
- linux——压缩解压命令学习简单笔记
- yaml 文件保存
- Jmeter 中多线程并发和循环控制器
- 获取scrollTop始终为0问题
- 手把手教你开发BLE数据透传应用程序
- English trip -- Review Unit3 Family 家人
- codeforces 1042d//Petya and Array// Codeforces Round #510 (Div. 2)
- 悟空CRM框架下载模板
- WaitForMultipleObjects用法详解
- 数据库,ADO.NET(ADO),Oledb(Odbc)和编程语言关系框架图