Reputation: 19651
How to match any thing dosen't contain a specific word using RegExp
Ex: Match any string doesn't contain 'aabbcc'
bbbaaaassdd // Match this
aabbaabbccaass // Reject this
Upvotes: 3
Views: 89
Reputation: 89557
You can use this to describe a substring that doesn't contain aabbcc:
(?>[^a]++|a(?!abbcc))*
for the whole string, just add anchors (^
$
)
Upvotes: 1
Reputation: 72971
If you're just after this sequence of characters, don't use a Regular Expression. Use strpos()
.
if (strpos('aabbaabbccaass', 'aabbcc') !== false) {
echo 'Reject this.'
}
Note: Be sure to read the warning in the manual about strpos()
return values.
Upvotes: 3
Reputation: 785058
You can use negative lookahead:
(?!.*?aabbcc)^.*$
PHP Code:
$str = 'aabbaabbccaass'; //or whatever
if (preg_match('/(?!.*?aabbcc)^.*$/', $str))
echo "accepted\n";
else
echo "rejected\n";
Upvotes: 2
Reputation: 18584
try this:
if(preg_match('/aabbcc/', $string) == 0) {
[ OK ]
}
else {
[ NOT OK ]
}
Upvotes: 1