Archive for November, 2008

Check if text doesn’t contain a specific word

November 10, 2008

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 hoแบทc word.


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

Thanks for reading,

Nguyen Minh Dzung.

Advertisements