【问题标题】:Regex for url rewrite with if then else用 if then else 重写 url 的正则表达式
【发布时间】:2012-04-22 22:49:16
【问题描述】:

鉴于这些网址:

1: http://site/page-name-one-123/
2: http://site/page-name-set2/
3: http://site/set20

我写了这个表达式将应用于最后一个 url 段

(?(?<=set[\d])([\d]+)|([^/]+))

我想要做的是仅当 url 段以 'set' 开头并且紧随其后的数字时才捕获后面跟着 'set' 的每个数字;否则我想使用整个段(不包括斜线)。

当我编写这个正则表达式时,它匹配任何不是“/”的字符。我认为我在测试语句中做错了什么。 有人能指点我吗?

谢谢

更新 感谢Josh 输入,我玩了一会儿,发现这个更适合我的需求:

set-(?P<number>[0-9]+)|(?P<segment>[^/]+)

【问题讨论】:

    标签: regex url-rewriting


    【解决方案1】:

    我希望这个模式可以帮助你,我根据你的要求把它放在一起。您可能想尝试将某些组设置为不捕获,以便仅获得所需的片段。但是,它确实会在开始时单独捕获您的 set URL,而没有 set

    ((?<=/{1})(((?<!set)[\w|-]*?)(\d+(?=/?))|((?:set)\d+)))
    

    如果需要,我建议使用RegExr 将其拆开。

    【讨论】:

      【解决方案2】:

      试试这个:

      ((?&lt;=/)set\d+|(?&lt;=/)[^/]+?set\d+)

      说明

      <!--
      Options: ^ and $ match at line breaks
      
      Match the regular expression below and capture its match into backreference number 1 «((?<=/)set\d+|(?<=/)[^/]+?set\d+)»
         Match either the regular expression below (attempting the next alternative only if this one fails) «(?<=/)set\d+»
            Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=/)»
               Match the character “/” literally «/»
            Match the characters “set” literally «set»
            Match a single digit 0..9 «\d+»
               Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
         Or match regular expression number 2 below (the entire group fails if this one fails to match) «(?<=/)[^/]+?set\d+»
            Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=/)»
               Match the character “/” literally «/»
            Match any character that is NOT a “/” «[^/]+?»
               Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?»
            Match the characters “set” literally «set»
            Match a single digit 0..9 «\d+»
               Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
      -->
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-26
        • 1970-01-01
        相关资源
        最近更新 更多