Regex for url rewrite with if then else

Given these URLs:

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

I wrote this expression that will be applied to the last segment of the URL :

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

What I would like to do is to catch every digit followed by “set” only if the URL segment starts with “set” and the number immediately after; otherwise, I want to use the entire segment (excluding slashes).

As I wrote this regular expression, it matches any character that is not a "/". I think I am doing something wrong in the test statement. Can someone point me right?

thank

UPDATE Thanks to the Josh tab, I played around a bit and found that this option suits me better:

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

, , . , , , . , URL .

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

RegExr, , .

+1

:

((?<=/)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) «+»
-->
0

All Articles