regexpgroupmatchtest.groovy

来自「Groovy动态语言 运行在JVM中的动态语言 可以方便的处理业务逻辑变化大的业」· GROOVY 代码 · 共 101 行

GROOVY
101
字号
/**
 * 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: 2711 $
 */

import java.util.regex.Matcher
import java.util.regex.Pattern

class 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 + =
减小字号Ctrl + -
显示快捷键?