gpt4 book ai didi

regex - 正则表达式从斜杠提取内容到行尾,斜杠或查询字符串

转载 作者:行者123 更新时间:2023-12-01 22:08:48 25 4
gpt4 key购买 nike

我试图将任意深度的路径中的最后一个斜杠匹配到该路径的末尾。我如何调整此示例,使之匹配?

([^/]+[.html]?)/?\?
  • https://www.somedomain/path/some-slug-94690(不匹配)
  • https://www.somedomain.com/one/somepath/another-slug?test(另一个子弹)
  • https://www.anotherdomain.com/somepath/another-slug/?test(另一个子弹)
  • https://www.anotherdomain.com/path/another_slug.html?test=true(另一个子弹)

  • https://regex101.com/r/bFz9mw/1

    最佳答案

    以下模式似乎有效:

    /[^?/]*/?(?=\?|$)

    Demo

    这是正则表达式的解释:
    /         match a (possibly) final path separator
    [^?/]* match zero or more non / or ? characters
    /? optionally match one more path separator
    (?=\?|$) which is followed by either `?` or the end of the string

    编辑:

    如果您的正则表达式版本不支持前瞻,请改用以下类似模式:
    /[^?/]*/?(?:\?|$)

    然后,修剪尾随的 ?(如果存在)。在Go上,您将使用 strings.TrimRight,并使用 ?作为要修剪的字符。

    关于regex - 正则表达式从斜杠提取内容到行尾,斜杠或查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59032337/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com