groovy-正则表达式

简介:

Groovy使用~”pattern” 来支持正则表达式,它将使用给定的模式字符串创建一个编译好的Java Pattern 对象。Groovy也支持 =~(创建一个Matcher)和 ==~ (返回boolean,是否给定的字符串匹配这个pattern)操作符。

对于groups的匹配, matcher[index] 是一个匹配到的group字符串的List或者string。

1 import java.util.regex.Matcher
2 import java.util.regex.Pattern
3 // ~ creates a Pattern from String
4 def pattern = ~/foo/
5 assert pattern instanceof Pattern
6 assert pattern.matcher("foo").matches() // returns TRUE
7 assert pattern.matcher("foobar").matches() // returns FALSE, because matches() must match whole String
8  
9 // =~ creates a Matcher, and in a boolean context, it's "true" if it has at least one match, "false" otherwise.
10 assert "cheesecheese" =~ "cheese"
11 assert "cheesecheese" =~ /cheese/
12 assert "cheese" == /cheese/ /*they are both string syntaxes*/
13 assert ! ("cheese" =~ /ham/)
14  
15 // ==~ tests, if String matches the pattern
16 assert "2009" ==~ /\d+/ // returns TRUE
17 assert "holla" ==~ /\d+/ // returns FALSE
18  
19 // lets create a Matcher
20 def matcher = "cheesecheese" =~ /cheese/
21 assert matcher instanceof Matcher
22  
23 // lets do some replacement
24 def cheese = ("cheesecheese" =~ /cheese/).replaceFirst("nice")
25 assert cheese == "nicecheese"
26 assert "color" == "colour".replaceFirst(/ou/, "o")
27  
28 cheese = ("cheesecheese" =~ /cheese/).replaceAll("nice")
29 assert cheese == "nicenice"
30  
31 // simple group demo
32 // You can also match a pattern that includes groups. First create a matcher object,
33 // either using the Java API, or more simply with the =~ operator. Then, you can index
34 // the matcher object to find the matches. matcher[0] returns a List representing the
35 // first match of the regular expression in the string. The first element is the string
36 // that matches the entire regular expression, and the remaining elements are the strings
37 // that match each group.
38 // Here's how it works:
39 def m = "foobarfoo" =~ /o(b.*r)f/
40 assert m[0] == ["obarf""bar"]
41 assert m[0][1] == "bar"
42  
43 // Although a Matcher isn't a list, it can be indexed like a list. In Groovy 1.6
44 // this includes using a collection as an index:
45  
46 matcher = "eat green cheese" =~ "e+"
47  
48 assert "ee" == matcher[2]
49 assert ["ee""e"] == matcher[2..3]
50 assert ["e""ee"] == matcher[02]
51 assert ["e""ee""ee"] == matcher[01..2]
52  
53 matcher = "cheese please" =~ /([^e]+)e+/
54 assert ["se""s"] == matcher[1]
55 assert [["se""s"], [" ple"" pl"]] == matcher[12]
56 assert [["se""s"], [" ple"" pl"]] == matcher[1 .. 2]
57 assert [["chee""ch"], [" ple"" pl"], ["ase""as"]] == matcher[0,2..3]
58 // Matcher defines an iterator() method, so it can be used, for example,
59 // with collect() and each():
60 matcher = "cheese please" =~ /([^e]+)e+/
61 matcher.each println it }
62 matcher.reset()
63 assert matcher.collect { it }?? ==
64  [["chee""ch"], ["se""s"], [" ple"" pl"], ["ase""as"]]
65 // The semantics of the iterator were changed by Groovy 1.6.
66 // In 1.5, each iteration would always return a string of the entire match, ignoring groups.
67 // In 1.6, if the regex has any groups, it returns a list of Strings as shown above.
68  
69 // there is also regular expression aware iterator grep()
70 assert ["foo""moo"] == ["foo""bar""moo"].grep(~/.*oo$/)
71 // which can be written also with findAll() method
72 assert ["foo""moo"] == ["foo""bar""moo"].findAll { it ==~ /.*oo/ }

More Examples

匹配每行开头的大写单词:

1 def before='''
2 apple
3 orange
4 y
5 banana
6 '''
7  
8 def expected='''
9 Apple
10 Orange
11 Y
12 Banana
13 '''
14  
15 assert expected == before.replaceAll(/(?m)^\w+/,
16  { it[0].toUpperCase() + ((it.size() > 1) ? it[1..-1] : '') })

匹配字符串中的每一个大写单词

1 assert "It Is A Beautiful Day!" ==
2  ("it is a beautiful day!".replaceAll(/\w+/,
3  { it[0].toUpperCase() + ((it.size() > 1) ? it[1..-1] : '') }))

使用 .toLowerCase() 让其他单词小写:

1 assert "It Is A Very Beautiful Day!" ==
2  ("it is a VERY beautiful day!".replaceAll(/\w+/,
3  { it[0].toUpperCase() + ((it.size() > 1) ? it[1..-1].toLowerCase() :'') }))

Gotchas

怎么使用String.replaceAll()的反向引用

GStrings 可能和你期望的不一样

1 def replaced = "abc".replaceAll(/(a)(b)(c)/, "$1$3")

产生一个类似于下面的错误:

[] illegal string body character after dollar sign:

解决办法:: either escape a literal dollar sign “\5orbracketthevalueexpression{5}” @ line []

Solution:

Use ‘ or / to delimit the replacement string:

1 def replaced = "abc".replaceAll(/(a)(b)(c)/, '$1$3')
目录
打赏
0
0
0
0
20
分享
相关文章
ubuntu 软 raid配置
ubuntu 软 raid配置
2237 2
解决在cmd中输入mongo出现‘mongo‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件的问题~
解决在cmd中输入mongo出现‘mongo‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件的问题~
1385 0
Angular状态管理神器ngrx Store:从零开始的实践指南与进阶优化秘籍,让你的前端应用状态井井有条、高效运行的绝招大揭秘
【8月更文挑战第31天】状态管理在现代Web应用开发中至关重要,特别是在构建大型、复杂的Angular应用时。ngrx Store借鉴Redux的设计理念,提供集中式状态管理和可预测的数据流,有助于增强应用的可维护性和可测试性。
276 0
Jenkins Pipeline 流水线 - 完整构建 Pipeline Script 脚本
Jenkins Pipeline 流水线 - 完整构建 Pipeline Script 脚本
227 0
Jenkins保姆级使用:Jenkins部署springboot项目,手把手实战经历
Jenkins是一个基于Java开发的开源的实现项目可持续集成的工具,解决了平时开发项目之后需要手动打包与发布项目的问题,将这个流程实现自动化;需要了解更多关于Jenkins的内容可以点击 官网 因为项目需要,近期要准备使用jenkins部署springboot单服务项目,详细记录一下其中遇到的问题和使用过程,留下脚印。
6865 0
公共医疗数据库汇总:无需实验,高效论文撰写利器
在医学研究领域,获取高质量的数据和文献资源是进行科学论文撰写的关键。随着信息技术的发展,以及公共医疗数据库的不断壮大和完善,研究人员可以轻松地获取大量的医学数据和文献信息,从而提高论文撰写的效率和质量。本文将为您介绍一系列全面的公共医疗数据库,这些数据库不仅为您提供了丰富的医学资源,还无需进行实验,成为高效论文撰写的利器。
3307 1
高效搜索与过滤:深入了解Linux命令`grep`
在Linux系统中,搜索和过滤文本是一项常见的任务,尤其在查找日志、配置文件或代码中特定内容时。`grep`命令作为一个强大的文本搜索工具,可以帮助你快速定位匹配的文本行。本文将详细介绍`grep`命令的基本概念、用法和一些常见技巧,帮助你更高效地搜索和过滤文本内容。
730 0
Flask+Redis实现登录权限管理
Flask+Redis实现登录权限管理
378 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问