3

鉴于这些网址:

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>[^/]+)
4

2 回答 2

1

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

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

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

于 2012-04-22T10:12:46.407 回答
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) «+»
-->
于 2012-04-22T10:39:32.057 回答