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 |
5 |
assert pattern instanceof Pattern |
6 |
assert pattern.matcher( "foo" ).matches() |
7 |
assert pattern.matcher( "foobar" ).matches() |
10 |
assert "cheesecheese" =~ "cheese" |
11 |
assert "cheesecheese" =~ /cheese/ |
12 |
assert "cheese" == /cheese/ |
13 |
assert ! ( "cheese" =~ /ham/) |
16 |
assert "2009" ==~ /d+/ |
17 |
assert "holla" ==~ /d+/ |
20 |
def matcher = "cheesecheese" =~ /cheese/ |
21 |
assert matcher instanceof Matcher |
24 |
def cheese = ( "cheesecheese" =~ /cheese/).replaceFirst( "nice" ) |
25 |
assert cheese == "nicecheese" |
26 |
assert "color" == "colour" .replaceFirst(/ou/, "o" ) |
28 |
cheese = ( "cheesecheese" =~ /cheese/).replaceAll( "nice" ) |
29 |
assert cheese == "nicenice" |
39 |
def m = "foobarfoo" =~ /o(b.*r)f/ |
40 |
assert m[ 0 ] == [ "obarf" , "bar" ] |
41 |
assert m[ 0 ][ 1 ] == "bar" |
46 |
matcher = "eat green cheese" =~ "e+" |
48 |
assert "ee" == matcher[ 2 ] |
49 |
assert [ "ee" , "e" ] == matcher[ 2 .. 3 ] |
50 |
assert [ "e" , "ee" ] == matcher[ 0 , 2 ] |
51 |
assert [ "e" , "ee" , "ee" ] == matcher[ 0 , 1 .. 2 ] |
53 |
matcher = "cheese please" =~ /([^e]+)e+/ |
54 |
assert [ "se" , "s" ] == matcher[ 1 ] |
55 |
assert [[ "se" , "s" ], [ " ple" , " pl" ]] == matcher[ 1 , 2 ] |
56 |
assert [[ "se" , "s" ], [ " ple" , " pl" ]] == matcher[ 1 .. 2 ] |
57 |
assert [[ "chee" , "ch" ], [ " ple" , " pl" ], [ "ase" , "as" ]] == matcher[ 0 , 2 .. 3 ] |
60 |
matcher = "cheese please" =~ /([^e]+)e+/ |
61 |
matcher. each { println it } |
63 |
assert matcher. collect { it }?? == |
64 |
[[ "chee" , "ch" ], [ "se" , "s" ], [ " ple" , " pl" ], [ "ase" , "as" ]] |
70 |
assert [ "foo" , "moo" ] == [ "foo" , "bar" , "moo" ]. grep (~/.*oo$/) |
72 |
assert [ "foo" , "moo" ] == [ "foo" , "bar" , "moo" ]. findAll { it ==~ /.*oo/ } |
More Examples
匹配每行开头的大写单词:
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 “$5″ or bracket the value expression “${5}” @ line []
Solution:
Use ‘ or / to delimit the replacement string:
1 |
def replaced = "abc" .replaceAll(/(a)(b)(c)/, '$1$3' ) |