首页 > 学院 > 开发设计 > 正文

Groovy正则表达式

2019-11-08 18:51:54
字体:
来源:转载
供稿:网友

Groovy正则表达式

1、~操作符可以方便地创建正则表达式。

p = ~"hello"PRintln p.getClass().name // java.util.regex.Pattern

2、为了匹配表达式,提供了=~和==~。=~进行部分匹配,==~进行完全匹配。

p = ~"hello"text = "hello world"if (text =~ p) {	println "match"} else {	println "not match"}if (text ==~ p) {	println "match"} else {	println "not match"}返回matchnot match3、=~返回一个Mather对象。
p = ~"(h|H)ello"text = "hello world! Hello BeiJing!"matcher = text =~ pprintln matcher.getClass().name // java.util.regex.Matcherprintln matcher.size() // 2println "${matcher[0]} and ${matcher[1]}" // [hello, h] and [Hello, H]

4、替代方法replace

p = ~"(h|H)ello"text = "hello world! Hello BeiJing!"result = (text =~ p).replaceAll("Hi")println result // Hi world! Hi BeiJing!


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表