Reputation: 1
I'm trying to match multiple URLs with the following structure:
/color-deals/best-color-deals/blue-deals/
I want the RegEx to grab all URLs with this structure regardless of the specific color at the end of the URL, but I need the colors included. For example, blue-deals, yellow-deals, red-deals, etc.
I've tried several combinations, but it doesn't seem very clean to me. Could someone confirm I did this in a way that's clean? The RegEx tester tells me it's grabbing what I want, but it still feels like it could be better.
\/color-deals\/best-color-deals\/((blue|yellow|green|pink)(\-deals\/$))
Upvotes: 0
Views: 40
Reputation: 27733
Your expression is just fine, if you might not want the capturing group, we can make it non-captured, and also for the ending trailing slash, just in case, if that might have been optional, we'd add a ?
so that it'd become so:
\/color-deals\/best-color-deals\/(?:blue|yellow|green|pink)-deals\/?$
Other than that, looks great.
If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.
jex.im visualizes regular expressions:
Upvotes: 1