Tag Archives: regular expression

Regex to Exclude Multiple Words

For Google Analytics, I wanted to create some chainable filters to deal with a set of subdomains that can occur on alpha, beta, and production, etc. So I needed to find a way to say “Give me all of the production subdomains, and none of the alpha/beta variants, or preprod.”

Here are the sample URLs that I want to filter out the alpha/beta/preprod from:

acme.domain.com
acme.beta123.domain.com
acme.alpha123.domain.com
wyle.alpha.domain.com
wyle.showme.domain.comcool_stuff.html
acme-two.domain.com
acme-two.preprod.domain.com

Here is the resultant regex:

^w+-*w*.(?!(?:alpha(123)*.|beta(123)*.|preprod.)domain.com).*$

And here is what Rubular looks like:
Rubular Multi Word Exclude URL Regex

Big shout out to Rubular.com, an awesome place to test out regex!