【问题标题】:How to match URL path using Google RE2 regex如何使用 Google RE2 正则表达式匹配 URL 路径
【发布时间】:2023-01-31 22:02:29
【问题描述】:

Google Cloud Platform 允许您使用 RE2 regex engine 创建标签日志。

如何创建与 URL 中的路径匹配的正则表达式?

例子匹配:

https://example.com/awesome                  --> "awesome"
https://example.com/awesome/path             --> "awesome/path"
https://example.com/awesome/path/            --> "awesome/path"
https://example.com/awesome/path?arg1=123    --> "awesome/path"

细节:

  • 域和协议是不变的,这里可以假设为https://example.com
  • 如果有多个目录,也应该匹配,包括中间的/
  • 不应匹配尾随的/
  • 查询,例如?arg1=123&arg2=456 不应匹配。
  • 可以假定目录名称将仅包含字母数字字符a-zA-Z0-9、破折号-和下划线_

请注意,Google RE2 不同于 PCRE2

【问题讨论】:

    标签: regex google-cloud-platform re2 google-logging


    【解决方案1】:

    所以语法不是 100% 清楚什么是支持的,什么不是。假设(不支持)VIM 意味着它受支持但不在 vim 上,我将从对你不关心的 url 开头的负面看法开始

    (?<=https://example.com/)
    

    然后你想要字母数字字符[w-]+后跟非尾随/所以我会添加一个前瞻性来验证/(?=/w+)/之后是否有字母数字字符

    完整的正则表达式

    (?<=https://example.com/)([w-]+((?=/w+)/|))+
    

    【讨论】:

      猜你喜欢
      • 2019-10-09
      • 2014-08-23
      • 2022-08-11
      • 1970-01-01
      • 2020-09-30
      • 1970-01-01
      • 2020-12-01
      • 2016-12-11
      • 2020-08-19
      相关资源
      最近更新 更多