There is such a pattern :

category\/(.+?)\/(:?size\/(.+?)\/)(?:page\/(.+?)\/?)? 

There are 3 groups of entries: category/(...)/ , size/(...)/ and optional page/(...)/ . When you try to make an optional group with size substitution ? after brackets - everything breaks down. Those. This pattern no longer works:

 category\/(.+?)\/(?:size\/(.+?)\/)?(?:page\/(.+?)\/?)? 

As a result, I would like the category value to always be recorded, and size and page only if they exist. What am I doing wrong?

  • Are you sure that the value of the first group should be catalog/istochniki-pitanija ? - Wiktor Stribiżew
  • Wiktor Stribiżew, yes, slash is acceptable there - tenRabbits

1 answer 1

The problem with greed. Tell the parser to look to the end of the line and everything will work.

 category\/(.+?)\/(?:size\/(.+?)\/)?(?:page\/(.+?)\/?)?$ 

https://regex101.com/r/wiIaNa/2

  • It helped, thanks! - tenRabbits