简体   繁体   English

Rego 正则表达式以匹配句子中的特定单词

[英]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".在句子“/man/service/man-aaaaaa-llll-latest/zzzn2-iii-ooo-x00_00-gg”中搜索单词“latest”。

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 测试“Rego”中的规则,只要句子中有“最新”一词,我就想将 boolean output 设为“真”。

Rego Playground link: https://play.openpolicyagent.org/p/e3vDQyYKlc Rego游乐场链接: 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:当前output:

{
    "hello": false
}

Expected output whenever Regex has a match for the word 'latest':每当 Regex 匹配“最新”一词时,预期为 output:

{
    "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请建议我是否必须使用任何其他正则表达式条件来实现预期结果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 .请参阅Rego playground 演示

Note that (t|)?请注意(t|)? is a capturing group that matches a t or empty string, and is basically equal to t?是一个匹配t或空字符串的捕获组,并且基本上等于t? pattern (there is no need to capture the t ) that matches an optional t char.匹配可选t字符的模式(无需捕获t )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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