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!
新聞熱點
疑難解答