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
It's possible to do all this in one regex, but you don't really need to. I think you'll have a better time if you run two separate tests: one for your include rules and one for your exclude rules. Not sure what language you're using, so I'll use JavaScript for the example:
function validate(str) {
var required = /\b(mobility|enterprise|products)\b/i;
var blocked = /\b(store|foo|bar)\b/i;
return required.test(str) && !blocked.test(str);
}
If you really want to do it in one pattern, try something like this:
/(?=.*\b(mobility|enterprise|products)\b)(?!.*\b(store|foo|bar)\b)(.+)/i
The i
at the end means case-insensitive, so use your language's equivalent if you're not using JavaScript.
All that being said, based on your description of the problem, I think what you REALLY want for this is string manipulation. Here's an example, again using JS:
function validate(str) {
var required = ['mobility','enterprise','products'];
var blocked = ['store','foo','bar'];
var lowercaseStr = str.toLowerCase(); //or just use str if you want case sensitivity
for (var i = 0; i < required.length; i++) {
if (lowercaseStr.indexOf(required[i]) === -1) {
return false;
}
}
for (var j = 0; j < blocked.length; j++) {
if (lowercaseStr.indexOf(blocked[j]) !== -1) {
return false;
}
}
}