I’m trying to capture data in a web url with regex

I’m trying to build my regex to match my urls

Here are 2 example urls

category/sorganiser/bouger/escalade/offre/78934/ category/sorganiser/savourer/offre/8040/

I would like to get the number just after offre (78934 and 8040) as well as the word just before the word offre (escalade and savourer)

I did several tests but did not pass

^category/(((w)+/){1,3})(d+)/?$

^category/(((w)+/){1,3})/offre/(d+)/?$

https://regex101.com/r/S4MTvK/1

Thank you

Answer

Instead of repeating a single word char in a group (w)+ you can repeat 1+ word chars in a single group (w+)

Note to not match the / before /offre as it is already matched in the iteration ^category/(?:(w+)/){1,3}


You can repeat the capture group inside a non capture group (?: to capture the last occurrence in the iteration.

^category/(?:(w+)/){1,3}offre/(d+)

The pattern matches

  • ^ Start of string
  • category/ Match literally
  • (?: Non capture group
    • (w+)/ Capture group 1, match 1+ word chars and match /
  • ){1,3} Close non capture, repeat 1-3 times and capture group 1 contains the last occurrence of 1+ word chars which is escalade or savourer
  • offre/ Match literally
  • (d+) Capture group 2, match 1+ digits

Regex demo

To also match an optional / before the end of the sting

^category/(?:(w+)/){1,3}offre/(d+)/?$

Regex demo