regex - 如何使用 re2 正则表达式否定字符串模式?

标签 regex prometheus grafana re2

我正在使用谷歌 re2用于查询 Prometheus 的正则表达式在 Grafana 仪表板上。尝试通过以下 3 种可能的输入字符串从键中获取值

 1. object{one="ab-vwxc",two="value1",key="abcd-eest-ed-xyz-bnn",four="obsoleteValues"}
 2. object{one="ab-vwxc",two="value1",key="abcd-eest-xyz-bnn",four="obsoleteValues"}
 3. object{one="ab-vwxc",two="value1",key="abcd-eest-xyz-bnn-ed",four="obsoleteValues"}

..验证如下所示

  • 应该包含abcd-
  • 不应包含 -ed

不知何故this regex

\bkey="(abcd(?:-\w+)*[^-][^e][^d]\w)"

..满足第一个条件abcd-但不能满足第二个条件(否定-ed)。

第二个输入选项的预期输出将是 abcd-eest-xyz-bnn。任何帮助将非常感激。非常感谢。

最佳答案

如果我正确理解您的要求,则以下模式应该有效:

\bkey="(abcd(?:-e|-(?:[^e\W]|e[^d\W])\w*)*)"

Demo .

重要部分分解:

(?:                 # Start a non-capturing group.
    -e              # Match '-e' literally.
    |               # Or the following...
    -               # Match '-' literally.
    (?:             # Start a second non-capturing group.
        [^e\W]      # Match any word character except 'e'.
        |           # Or...
        e[^d\W]     # Match 'e' followed by any word character except 'd'.
    )               # Close non-capturing group.
    \w*             # Match zero or more additional word characters.
)                   # Close non-capturing group.

或者简单来说:

Match a hyphen followed by:

  • only the letter 'e'. Or..
  • a word* not starting with 'e'. Or..
  • a word starting with 'e' not followed by 'd'.

*此处的“单词”表示正则表达式中定义的一串单词字符。

关于regex - 如何使用 re2 正则表达式否定字符串模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63061762/

相关文章:

prometheus - 禁用对特定主机的警报,同时对所有其他主机发出警报

kubernetes - 在 Prometheus Alertmanager 上设置 MicrosoftTeams 通知

go - 将 Prometheus 部署到 Cloud Foundry

javascript - 正则表达式匹配字符串中的单词,同时也匹配引号之间的字符串

grafana - 使用 Influxdb 在 Grafana 中选择字符串值作为整数

kubernetes - ETCD 向 Prometheus 认证数据

docker - 修改grafana查询以跳过一个Docker容器

php - 行尾的正则表达式

javascript - Firefox 扩展如何将字符串类型变量传递给 javascript 替换正则表达式参数?

java - 如何编写正则表达式来匹配\s但不\\\s