Before reading this article, I highly recommend reading my previous parts:
- Introduction to Regular Expressions
- Regular Expressions Part 2: Grouping, Quantifiers and Character Classes
This article will focus on how to tell the regular expression engine to ignore certain characters, or patterns of characters.
We will also explore how to tell the engine to look for a certain pattern of text when that pattern is found to the right or to the left of the rest of a string.
One form of negation is supported in character classes.
The following is an example of a regular expression:
- var myExpression = new Regex(@"[^aeiou]");
Another form of negation can be seen when using the special character classes.
Here is a list of the syntax that can be typed into a Regex object:
\S Do not match white space characters.
\D Do not match any digit.
\W Do not match any "word" character (in other words, a through z, A through Z, the _ character and numbers).
Another concept that goes hand in hand with negation is called assertions. Assertions are used to look for a pattern that is found to the left or to the right of the rest of a string.
Here's an example:
- var myExpression = new Regex(@"(q(?!p))");
The expression inside the group means that the engine should match the letter q, if it is not next to the letter p.
Every time we want to say, "x not followed by y," we use the syntax ?! followed by the expression we want to ignore.
This needs to be enclosed in parentheses. That is the reason for our second set of parentheses in our example above.
How, then, should we tell the regular expression engine to search for the letter q, only if it is next to the letter p?
The only change that needs to be made is swapping the character ! for the character =.
Here's the modified example:
- var myExpression = new Regex(@"(q(?=p))");

Sonu ChaudharyPosted Feb 25, 2016, 7:14 AM
nice article
Santhakumar MunuswamyPosted Jul 28, 2015, 3:06 PM
Thanks for sharing
Sibeesh VenuPosted Jul 28, 2015, 7:53 AM
Nice Share
Nilesh JadavPosted Jul 28, 2015, 6:24 AM
Nice one sir
Debasis SahaPosted Jul 28, 2015, 4:49 AM
Good One..
Gowtham RajamanickamPosted Jul 28, 2015, 4:29 AM
good one..
Gopi ChandPosted Jul 28, 2015, 4:00 AM
Good start up...welcome
Rahul Kumar SaxenaPosted Jul 28, 2015, 3:52 AM
Good Show
Rajeesh MenothPosted Jul 28, 2015, 3:23 AM
Good One