groovy-正则表达式

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

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

1import java.util.regex.Matcher
2import java.util.regex.Pattern
3// ~ creates a Pattern from String
4def pattern = ~/foo/
5assert pattern instanceof Pattern
6assert pattern.matcher("foo").matches() // returns TRUE
7assert 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.
10assert "cheesecheese" =~ "cheese"
11assert "cheesecheese" =~ /cheese/
12assert "cheese" == /cheese/ /*they are both string syntaxes*/
13assert ! ("cheese" =~ /ham/)
14 
15// ==~ tests, if String matches the pattern
16assert "2009" ==~ /\d+/ // returns TRUE
17assert "holla" ==~ /\d+/ // returns FALSE
18 
19// lets create a Matcher
20def matcher = "cheesecheese" =~ /cheese/
21assert matcher instanceof Matcher
22 
23// lets do some replacement
24def cheese = ("cheesecheese" =~ /cheese/).replaceFirst("nice")
25assert cheese == "nicecheese"
26assert "color" == "colour".replaceFirst(/ou/, "o")
27 
28cheese = ("cheesecheese" =~ /cheese/).replaceAll("nice")
29assert 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:
39def m = "foobarfoo" =~ /o(b.*r)f/
40assert m[0] == ["obarf""bar"]
41assert 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 
46matcher = "eat green cheese" =~ "e+"
47 
48assert "ee" == matcher[2]
49assert ["ee""e"] == matcher[2..3]
50assert ["e""ee"] == matcher[02]
51assert ["e""ee""ee"] == matcher[01..2]
52 
53matcher = "cheese please" =~ /([^e]+)e+/
54assert ["se""s"] == matcher[1]
55assert [["se""s"], [" ple"" pl"]] == matcher[12]
56assert [["se""s"], [" ple"" pl"]] == matcher[1 .. 2]
57assert [["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():
60matcher = "cheese please" =~ /([^e]+)e+/
61matcher.each println it }
62matcher.reset()
63assert 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()
70assert ["foo""moo"] == ["foo""bar""moo"].grep(~/.*oo$/)
71// which can be written also with findAll() method
72assert ["foo""moo"] == ["foo""bar""moo"].findAll { it ==~ /.*oo/ }

More Examples

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

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

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

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

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

1assert "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 可能和你期望的不一样

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

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

[] illegal string body character after dollar sign:

解决办法:: either escape a literal dollar sign “\$5″ or bracket the value expression “${5}” @ line []

Solution:

Use ‘ or / to delimit the replacement string:

1def replaced = "abc".replaceAll(/(a)(b)(c)/, '$1$3')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值