Reputation: 1559
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?
Upvotes: 0
Views: 181
Reputation: 626893
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.
Upvotes: 2