簡體   English   中英

Rego 正則表達式以匹配句子中的特定單詞

[英]Rego regex to match a specific word from a sentence

我寫了一個正則表達式

 \blates(t|)?\b

在句子“/man/service/man-aaaaaa-llll-latest/zzzn2-iii-ooo-x00_00-gg”中搜索單詞“latest”。

我正在通過 Rego playground 測試“Rego”中的規則,只要句子中有“最新”一詞,我就想將 boolean output 設為“真”。

Rego游樂場鏈接: https://play.openpolicyagent.org/p/e3vDQyYKlc

重新輸入

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

雷哥規則:

package play

default hello = false

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

當前output:

{
    "hello": false
}

每當 Regex 匹配“最新”一詞時,預期為 output:

{
    "hello": true
}

請建議我是否必須使用任何其他正則表達式條件來實現預期結果https://www.openpolicyagent.org/docs/latest/policy-reference/#regex

您可以使用

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

或者

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

請參閱Rego playground 演示

請注意(t|)? 是一個匹配t或空字符串的捕獲組,並且基本上等於t? 匹配可選t字符的模式(無需捕獲t )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM