faressoft
faressoft

Reputation: 19651

How to match any thing dosen't contain a specific word using RegExp

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

Answers (5)

Casimir et Hippolyte
Casimir et Hippolyte

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

mlwacosmos
mlwacosmos

Reputation: 4541

try this to avoid some sequences of letters :

^((?!aabbcc).)*$

Upvotes: 2

Jason McCreary
Jason McCreary

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

anubhava
anubhava

Reputation: 785058

You can use negative lookahead:

(?!.*?aabbcc)^.*$

Live Demo: http://www.rubular.com/r/4Exbf7UdDv

PHP Code:

$str = 'aabbaabbccaass'; //or whatever
if (preg_match('/(?!.*?aabbcc)^.*$/', $str))
   echo "accepted\n";
else
   echo "rejected\n";

Upvotes: 2

Matteo Tassinari
Matteo Tassinari

Reputation: 18584

try this:

if(preg_match('/aabbcc/', $string) == 0) {
   [ OK ]
}
else {
   [ NOT OK ]
}

Upvotes: 1

Related Questions