I've written a regex
\blates(t|)?\b
to search for a word "latest" in a sentence "/man/service/man-aaaaaa-lllll-latest/zzzn2-iii-ooo-x00_00-gg".
I'm testing a rule in 'Rego' through Rego playground, whenever there's a word 'latest' in a sentence, I want to get a boolean output as 'true'.
Rego Playground link: https://play.openpolicyagent.org/p/e3vDQyYKlc
Rego input
{
"message": "/man/service/man-aaaaaa-lllll-latest/zzzn2-iii-ooo-x00_00-gg"
}
Rego rule:
package play
default hello = false
hello {
m := input.message
regex.match("\blates(t|)?\b", m)
}
Current output:
{
"hello": false
}
Expected output whenever Regex has a match for the word 'latest':
{
"hello": true
}
Please suggest if I've to use any other Regex condition to achieve the expected result https://www.openpolicyagent.org/docs/latest/policy-reference/#regex
CodePudding user response:
You can use
regex.match("\\blatest?\\b", m)
or
regex.match(".*\\blatest?\\b.*", m)
See the Rego playground demo.
Note that (t|)?
is a capturing group that matches a t
or empty string, and is basically equal to t?
pattern (there is no need to capture the t
) that matches an optional t
char.