简体   繁体   中英

Rego regex to match a specific word from a sentence

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

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.

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