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, 2:14 AM EST
nice article
Santhakumar MunuswamyPosted Jul 28, 2015, 11:06 AM EDT
Thanks for sharing
Sibeesh VenuTOP 100Posted Jul 28, 2015, 3:53 AM EDT
Nice Share
Nilesh JadavPosted Jul 28, 2015, 2:24 AM EDT
Nice one sir
Debasis SahaTOP 100Posted Jul 28, 2015, 12:49 AM EDT
Good One..
Gowtham RajamanickamTOP 100Posted Jul 28, 2015, 12:29 AM EDT
good one..
Gopi ChandPosted Jul 28, 2015, 12:00 AM EDT
Good start up...welcome
Rahul Kumar SaxenaTOP 100Posted Jul 27, 2015, 11:52 PM EDT
Good Show
Rajeesh MenothPosted Jul 27, 2015, 11:23 PM EDT
Good One