Help with regex include and exclude

Tom picture Tom · Mar 15, 2011 · Viewed 18.8k times · Source

I would like some help with regex.

I'm trying to create an expression that will include certain strings and exclude certain strings.

For example:

I would like to include any URL containing mobility http://www.something.com/mobility/

However I would like to exclude any URL containing store http://www.something.com/store/mobility/

FYI I have many keywords that I'm using to include. Currently I am including like this /mobility|enterprise|products/i however I am not finding it able to exclude links that contain other keywords.

Thank you in advance for any help and insight you can provide.

_t

Answer

codaddict picture codaddict · Mar 15, 2011

To match a string which must have word from a set of words you can use positive lookahead as:

^(?=.*(?:inc1|inc2|...))

To not match a string which has a word from a list of stop words you can use negative lookahead as:

^(?!.*(?:ex1|ex2|...))

You can combine the above two requirements in single regex as:

^(?=.*(?:inc1|inc2|...))(?!.*(?:ex1|ex2|...))REGEX_TO_MATCH_URL$

Rubular link