reReplaceAll RE flavor

What flavor of RE is the notification template using ?
I’m having issues with even simple RE expressions
For example the expression below (valid with Java RE and golang) throws an error
var=‘C’.*value=(\d+.\d).*var=‘D’.*value=(\d+.\d)

I would say golang. But are you sure that you have string not object as input.

Tried with both and it works to an extent.
I’m trying to parse the .ValueString:

  "__value_string__": "[ var='C' labels={impianto=Aggius, wtg=5} value=22.8 ], [ var='D' labels={impianto=Aggius, wtg=5} value=18.9 ], [ var='E' labels={impianto=Aggius, wtg=5} value=1 ], [ var='F' labels={impianto=Aggius, wtg=5} value=0 ], [ var='G' labels={impianto=Aggius, wtg=5} value=1 ]",

On a web tool to test RE, it parses ok and gives me 2 capture groups: 22.8 and 18.9. as expected.

In Grafana I get the following error:

ERROR:
invalid_template
template: :3: invalid syntax

{{range .Alerts}}
{{reReplaceAll “var=‘C’.value=(\d+.\d).?var=‘D’.*value=(\d+.\d)” “Found: $1 and $2” .ValueString}}
{{end}}

Note that the ’ are the proper ones in the RE

Could you share your regex test Golang example on https://play.golang.org pls?

Not sure I understand. I’m not writing anything in Golang, just using the alerting template with a regex.
I used the online tool: https://regex101.com set with Golang flavor and with this RE expression:
.*var=‘C’.*value=(\d+.\d).*var=‘D’.value=(\d+.\d).

Yes, you are not writing in golang, but it will be excuted in golang, so golang playground is better. It nice that you can test i wit regex101, but some characters may need to be escaped, so test it with golang.

Not sure if that is what you meant but here you go:

// You can edit this code!
// Click here and start typing.
package main

import (
	"fmt"
	"regexp"
)

func main() {
	str := "\"__value_string__\": \"[ var='C' labels={impianto=Aggius, wtg=5} value=22.8 ], [ var='D' labels={impianto=Aggius, wtg=5} value=18.9 ], [ var='E' labels={impianto=Aggius, wtg=5} value=1 ], [ var='F' labels={impianto=Aggius, wtg=5} value=0 ], [ var='G' labels={impianto=Aggius, wtg=5} value=1 ]\","
		pattern, compileErr := regexp.Compile(".*?var='C'.*?value=(\d+.\d).*var='D'.*?value=(\d+.\d).*")
	//	pattern, compileErr := regexp.Compile(".*var='C'.*value=([[:digit:]]+.[[:digit:]]).*var='D'.*value=([[:digit:]]+.[[:digit:]]).*")
	// 	pattern, compileErr := regexp.Compile(".*?var='C'.*?value=([[:digit:]]+.[[:digit:]]).*?var='D'.*?value=([[:digit:]]+.[[:digit:]]).*")
	//	pattern, compileErr := regexp.Compile("value=([[:digit:]]+.[[:digit:]])")
	if compileErr == nil {
		fmt.Println("Result:", pattern.FindAllString(str, 2))
		fmt.Println("Result replace:", pattern.ReplaceAllString(str, "REPLACEFOUND"))
	}
}

I run a few tests with the following results:

with: pattern, compileErr := regexp.Compile(“value=([[:digit:]]+.[[:digit:]])”)
Result: [value=22.8 value=18.9]
Result replace: “value_string”: “[ var=‘C’ labels={impianto=Aggius, wtg=5} REPLACEFOUND ], [ var=‘D’ labels={impianto=Aggius, wtg=5} REPLACEFOUND ], [ var=‘E’ labels={impianto=Aggius, wtg=5} value=1 ], [ var=‘F’ labels={impianto=Aggius, wtg=5} value=0 ], [ var=‘G’ labels={impianto=Aggius, wtg=5} value=1 ]”,

with: pattern, compileErr := regexp.Compile(“.*var=‘C’.*value=([[:digit:]]+.[[:digit:]]).*var=‘D’.value=([[:digit:]]+.[[:digit:]]).”)
Result: [“value_string”: “[ var=‘C’ labels={impianto=Aggius, wtg=5} value=22.8 ], [ var=‘D’ labels={impianto=Aggius, wtg=5} value=18.9 ], [ var=‘E’ labels={impianto=Aggius, wtg=5} value=1 ], [ var=‘F’ labels={impianto=Aggius, wtg=5} value=0 ], [ var=‘G’ labels={impianto=Aggius, wtg=5} value=1 ]”,]
Result replace: REPLACEFOUND

with: pattern, compileErr := regexp.Compile(“.?var=‘C’.?value=([[:digit:]]+.[[:digit:]]).?var=‘D’.?value=([[:digit:]]+.[[:digit:]]).*”)
Result: [“value_string”: “[ var=‘C’ labels={impianto=Aggius, wtg=5} value=22.8 ], [ var=‘D’ labels={impianto=Aggius, wtg=5} value=18.9 ], [ var=‘E’ labels={impianto=Aggius, wtg=5} value=1 ], [ var=‘F’ labels={impianto=Aggius, wtg=5} value=0 ], [ var=‘G’ labels={impianto=Aggius, wtg=5} value=1 ]”,]
Result replace: REPLACEFOUND

with: pattern, compileErr := regexp.Compile(“.?var=‘C’.?value=(\d+.\d).var=‘D’.?value=(\d+.\d).*”)
Error: ./prog.go:12:63: unknown escape – it seems to refer to the \d

Please note that in the string the double quotes need to be escaped

Please use formatted text (you can edit your posts and fix that) + you can share code on play golang directly.
Your code: Go Playground - The Go Programming Language doesn’t work, error ./prog.go:12:63: unknown escape Are you really sure that’s correct regex?

Fix: escape \d:
regexp.Compile(".*?var='C'.*?value=(\d+.\d).*var='D'.*?value=(\d+.\d).*")

regexp.Compile(".*?var='C'.*?value=(\\d+.\\d).*var='D'.*?value=(\\d+.\\d).*")

BTW: Why you need to all these regex games in the template? Why you just don’t print values in the annotation and print it in the template. Why you are trying it in such complicated way?

Here is source code for that reReplaceAll, so it is Golang definitely:

Thanks,
I ended up going with your suggestion of using the annotations.
On another note, even after fixing the regex by escaping the \d, still it doesn’t behave as expected as the first .* captures everything

Yeah, beacause that is going to template, which may need another escaping,… So I believe it is not simple.

This is really bad approach, which doesn’t scale, e. g. you may write correct regexp for this particular query, but alert may have 2-x many queries, expressions/classic conditions, which your regex doesn’t cover. Template should have minimal logic, no parsing.

1 Like