0

I have a string:

produits/mousse-de-montage/marque/pbb/item/promax-pro-expansion

Everything that goes after produits is optional and must be captured by groups, here is my regex

/^.*produits(.*)?(\/marque\/.*)?(\/item\/.*)?/g

For some strange reason, he put those 3 groups into a single one. If i remove question mark, he will split in 3 groups.

Bright Sun
  • 39
  • 4
  • It is not a strange reason, the `.*` eats up everything and the question marks make two thirds of your string optional. Simply split on `/` and you should be fine here. – Jan Aug 12 '17 at 07:55
  • @Jan but as i said, those are optional and might not be present. If i remove question mark, he will split in 3 groups. – Bright Sun Aug 12 '17 at 08:00
  • Try [`^.*?produits(.*?)(\/marque\/.*?)?(\/item\/.*?)?$`](https://regex101.com/r/3YRiuc/1). – Wiktor Stribiżew Aug 12 '17 at 08:29
  • @Wiktor Stribiżew thanks, works perfectly. – Bright Sun Aug 12 '17 at 09:22
  • @Wiktor Stribiżew Final regex ^.*?produits(.*?|)((?:\/marque\/)(.*?)|)?((?:\/item\/)(.*?)|)?$ to keep 3 groups and exlude marque and item – Bright Sun Aug 12 '17 at 09:39

0 Answers0