Regex to match path containing one of two strings

939 Views Asked by At

RegEx to match one of two strings in the third segment, ie in pseudo code:

/content/au/(boomer or millenial)/...

Example matches

/content/au/boomer 
/content/au/boomer/male/31 
/content/au/millenial/female/29/M 
/content/au/millenial/male/18/UM

Example non-matches

/content/au
/content/nz/millenial/male/18/UM
/content/au/genz/male

I've tried this, but to no avail:

^/content/au/(?![^/]*/(?:millenial|boomer))([^/]*)
3

There are 3 best solutions below

2
On BEST ANSWER

Don't use a look ahead; just use the plain alternation millenial|boomer then a word-boundary:

^/content/au/(?:millenial|boomer)\b(?:/.*)?

See live demo.

You should probably spell millennial correctly too (two "n"s, not one).

0
On

You can use the following regex DEMO

content/au/(?:boomer|millenial)

enter image description here

0
On

What's with the negative lookahead? This is a simple, if not trivial, positive match.

^/content/au/(?:millenial|boomer)(?:/|$)

The final group says the match needs to be followed by a slash or nothing, so as to exclude paths which begin with one of the alternatives, but contain additional text.