简体   繁体   中英

How do I use extended regular expression grouping in groovy?

I have this sed command that can pull a number from an HTML report on code coverage from Coverlet:

sed -rn 's/(<tr><th>Branch coverage:<\/th><td>([0-9]+.?[0-9]+)%<\/td><\/tr>)+/\2/p' /output/report/index.htm

When I try it in groovy it fails on the \2 with

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/private/tmp/coverage.groovy: 19: Unexpected input: '\'; Expecting <EOF> @ line 19, column 81.
   0-9]+.?[0-9]+)%<\/td><\/tr>)+/\2/

This is the groovy code failing:

def regex = /(<tr><th>Branch coverage:<\/th><td>([0-9]+.?[0-9]+)%<\/td><\/tr>)+/\2/

new File('/tmp/output/report/index.htm').eachLine { line ->
    if ((match = line =~ regex)) {
        println match
    }
}

I'm not sure how to escape it I guess?

The sed -rn 's/(<tr><th>Branch coverage:<\/th><td>([0-9]+.?[0-9]+)%<\/td><\/tr>)+/\2/p' /output/report/index.htm contains a substitution command with -n option and p flag that are both used to extract a certain value from a line using sed .

In Groovy, you should use extracting .find() method directly and grab any group you need from the match result. Here is a demo:

String line = '<tr><th>Branch coverage:</th><td>15.50%</td></tr>'
def m = line =~ /(<tr><th>Branch coverage:<\/th><td>([0-9]+(?:\.[0-9]+)?)%<\/td><\/tr>)+/
if(m.find()) {         // If the first match is found
    println m[0][2]    // Show Group 2 value of the first match
} else {
    println 'No match' // Else, print No match
}

See a Groovy demo printing 15.50 as a result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM