I need help setting up a regex that has a very specific start, with a capturing group, and then other groups that can be in any order.
Example:
c/category-name/f-brand-super-brand/f-hair-porosity-POR
category => category-name
f-brand => super-brand
f-hair-porosity => POR
c/exampleC/f-brand-brandY/f-hair-porosity-my-porosity
category => exampleC
f-brand => brandY
f-hair-porosity => my-porosity
This is how far I've come:
(f-(brand|hair-porosity|shampoo-type)?-?(.*?)(\/|$))
By using this regex I can find the matches I need and it works in any given f-xxx order.
However the problem I am facing is that I can't capture the category name. All my attempts result in either capturing just the first f-xxx or the last f-xxx.
Any ideas?
This will go in a wordpress add_rewrite_url, so I need it to be a one liner.
Thanks!
CodePudding user response:
The category prefix has a different pattern, ending with a forward slash (c/
), so you should add an option for that.
I would suggest this regular expression:
(\bc\/|\bf-brand-|\bf-hair-porosity-|\bf-shampoo-type-)((?:(?!\/f-).)*)
This will capture two groups:
* the prefix
* the value
It will allow /
to occur inside a single match when it is not followed by ´f-´. This is where a subcategory could be included in the category match.