Search code examples
regexexpressionengine

Regular expression that matches "articles" but not "articles-main"?


How would I write a regular expression that matches any URL containing the segment "articles" followed by any other segment, but does NOT match URLs with the segment "articles-main"?

So it would match these:

www.mysite.com/articles
www.mysite.com/articles/tinman

But not these:

www.mysite.com/articles-main
www.mysite.com/articles-main/tinman

This is for use in ExpressionEngine if it matters.


Solution

  • Use a so called negative lookahead:

    articles(?!-main)
    

    or more precisely:

    mysite.com/articles(?!-main)