Check if text doesn’t contain a specific word

Hi all,

Great to be back from a period of nightmare. Today, I tell you how to use Regular Expression to check if a text doesn’t contain a specific word. If you are yet to know about Regular Expression, just pick one book about it and read, you will find it is very useful and interesting.

Now, we can’t use Character Class […] to check if a text does n’t contain a specific word. E.g, we have to check if input text doesn’t contain word “Sparklite”, we have to use Look around negative. And pattern to use is: ^((?!Sparklite).)*$

In short, for general case, the pattern is: ^((?!theword).)*$. This pattern can be extended to: ^((?!thepattern).)*$

For further reference, I list the syntax of Look around in Regular Expression here

Look Ahead: q(?=u): q is followed by u

Look Ahead Negative: q(?!u): q is not followed by u

Look Behind: (?<=q)u: u is preceded by q

Look Behind Nevative: (?<!q)u: u is not preceded by q.

q, u: pattern hoc word.

I’ll present the way that a Regular Expression engine works in another article. So stay tuned.

Thanks for reading,

Nguyen Minh Dzung.


Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: