正则表达式用url重写if if else

时间:2012-04-22 09:41:47

标签: regex url-rewriting

鉴于这些网址:

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

我写了这个表达,将应用于最后一个网址

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

我想要做的是只有当网址段以'set'开头并且紧接着后面的数字时才会捕获每个数字后跟'set';否则我想使用整个段(不包括斜杠)。

当我写这个正则表达式时,它匹配任何不是'/'的字符。我想我在测试声明中做错了。 有人能指出我吗?

由于

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

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

2 个答案:

答案 0 :(得分:1)

我希望这种模式可以帮助你,我根据你的要求把它放在一起。您可能想要将一些组设置为不捕获,以便您只获得所需的段。但是,它会在开始时单独捕获您的设置网址而不用设置

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

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

答案 1 :(得分:0)

试试这个:

((?<=/)set\d+|(?<=/)[^/]+?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) «+»
-->