1、~操作符可以方便地创建正则表达式。
p = ~"hello"PRintln p.getClass().name // java.util.regex.Pattern2、为了匹配表达式,提供了=~和==~。=~进行部分匹配,==~进行完全匹配。
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!
新闻热点
疑难解答