Search code examples
regexalternation

Regex to match path containing one of two strings


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))([^/]*)

Solution

  • 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).