rego Regex匹配句子的特定单词

发布于 2025-01-21 22:32:26 字数 1037 浏览 5 评论 0原文

我已经写了一条正则

 \blates(t|)?\b

搜索句子中的单词“最新”/man/service/man-aaaaaa-llllll-latest/zzzn2-iii-ooo-x00_00-gg”。

我正在通过Rego Playground测试“ Rego”中的规则,每当句子中有一个单词“最新”时,我都想将布尔输出作为“ True”。

Playground链接: https://play.openpoly.openpolycyagent.orgg/p/p/p/p/p/p/p/pp/e3vdqyyyklc

Reco

{
    "message": "/man/service/man-aaaaaa-lllll-latest/zzzn2-iii-ooo-x00_00-gg"
}

grougeo规则:

package play

default hello = false

hello {
    m := input.message
    regex.match("\blates(t|)?\b", m)
}

当前输出:

{
    "hello": false
}

每当REGEX与“最新”一词的匹配时,预期输出:

{
    "hello": true
}

如果我使用任何其他正则条件来达到预期结果 https://www.openpolicyagent.org/docs/latest/policy-reference /#REGEX

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天生の放荡 2025-01-28 22:32:26

您可以使用

regex.match("\\blatest?\\b", m)

regex.match(".*\\blatest?\\b.*", m)

查看 rego playgoground demo

请注意,(t |)?是匹配t或空字符串的捕获组,并且基本上等于t?模式(那里无需捕获与可选t char匹配的t)。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文