📄 regexpgroupmatchtest.groovy
字号:
/** * Test for fixing the Jira issue GROOVY-1000 * * Fix an infinite loop when getting after group matching in regular expression. * Graham Miller has given this idea. * * @author Pilho Kim * @version $Revision: 1.1 $ */import java.util.regex.Matcherimport java.util.regex.Patternclass RegExpGroupMatchTest extends GroovyTestCase { void testFirst() { assert "cheesecheese" =~ "cheese" assert "cheesecheese" =~ /cheese/ assert "cheese" == /cheese/ /*they are both string syntaxes*/ } void testSecond() { // Lets create a regex Pattern def pattern = ~/foo/ assert pattern instanceof Pattern assert pattern.matcher("foo").matches() } void testThird() { // Lets create a Matcher def matcher = "cheesecheese" =~ /cheese/ assert matcher instanceof Matcher def answer = matcher.replaceAll("edam") println answer } void testFourth() { // Lets do some replacement def cheese = ("cheesecheese" =~ /cheese/).replaceFirst("nice") assert cheese == "nicecheese" } void testFifth() { // Group demo def matcher = "\$abc." =~ "\\\$(.*)\\." matcher.matches(); // must be invoked assert matcher.group(1) == "abc" // is one, not zero // assert matcher[1] == "abc" // This has worked only before jsr-03-release println (matcher[0]) assert matcher[0] == ["\$abc.", "abc"] assert matcher[0][1] == "abc" } void testSixth() { // Group demo // Avoid having to double all the backslash escaping characters. def matcher = "\$abc." =~ /\$(.*)\./ // no need to double-escape! assert "\\\$(.*)\\." == /\$(.*)\./ matcher.matches(); // must be invoked assert matcher.group(1) == "abc" // is one, not zero // assert matcher[1] == "abc" // This has worked only before jsr-03-release println (matcher[0]) assert matcher[0] == ["\$abc.", "abc"] assert matcher[0][1] == "abc" } // Test no group match. void testNoGroupMatcherAndGet() { def p = /ab[d|f]/ def m = "abcabdabeabf" =~ p for (i in 0..<m.count) { println( "m.groupCount() = " + m.groupCount()) println( " " + i + ": " + m[i] ) // m[i] is a String } } // Test group matches. void testGroupMatcherAndGet() { def p = /(?:ab([c|d|e|f]))/ def m = "abcabdabeabf" =~ p for (i in 0..<m.count) { println( "m.groupCount() = " + m.groupCount()) println( " " + i + ": " + m[i] ) // m[i] is a String } } // Test group matches. void testAnotherGroupMatcherAndGet() { def m = "abcabdabeabfabxyzabx" =~ /(?:ab([d|x-z]+))/ m.count.times { println( "m.groupCount() = " + m.groupCount()) println( " " + it + ": " + m[it] ) // m[it] is a String } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -