lundi 13 juin 2016

The matcher regex behaviour in groovy [duplicate]

This question already has an answer here:

First of all, there is a little test code showing my problem

    @Test
public void test() {


    String text = """
Red Hat Enterprise Linux Desktop (v. 5 client):

Source:
http://ift.tt/1PWTd7q

i386:
cups-1.3.7-30.el5.i386.rpm
cups-debuginfo-1.3.7-30.el5.i386.rpm
cups-libs-1.3.7-30.el5.i386.rpm
cups-lpd-1.3.7-30.el5.i386.rpm

x86_64:
cups-1.3.7-30.el5.x86_64.rpm
cups-debuginfo-1.3.7-30.el5.i386.rpm
cups-debuginfo-1.3.7-30.el5.x86_64.rpm
cups-libs-1.3.7-30.el5.i386.rpm
cups-libs-1.3.7-30.el5.x86_64.rpm
cups-lpd-1.3.7-30.el5.x86_64.rpm
"""

        Pattern PACKAGE_REDHAT_PATTERN = Pattern.compile(/([^\/\n]*)-([^-]*)-([^-]*)\.([^.]*)\.rpm$/,Pattern.MULTILINE)

        // First test
        def matchedTest1 = text.findAll(PACKAGE_REDHAT_PATTERN)

        // Second test

        ArrayList<String> matchedTest2 = new ArrayList<String>()

        Matcher matcher = PACKAGE_REDHAT_PATTERN.matcher(text);

        if(matcher.matches())
        {
            while(matcher.find())
            {
                //          println('match 0 : '+match.group(0))
                //          println('match 1 : '+match.group(1))
                //          println('match 2 : '+match.group(2))
                //          println('match 3 : '+match.group(3))
                //          println('match 4 : '+match.group(4))
                //
                matchedTest2.add(matcher.group(1))
            }
        }

        println'end'
    }

I don't understand why in the first test i have 11 results (as expected) and no match in the second test.

I must have missed something big in the use/init of the matcher.

Thank you all for your time and answer

Aucun commentaire:

Enregistrer un commentaire