Oct 29, 2007

Find Complete line NOT matched by the regular expression

Some times it’s required get all the instances which are not in the match string.
For example consider the following string.

I think that Michel Smith
is a fictional character. His real name
might be Michel Jackson, Michel Westling,
or Michel Liu for all we know.

If we are interested to get all lines which does not consist of word “Michel”, we can use Regex in .net and create a regular expression as follows

“^(?:(?!(.*Michel.*)).)*$\r?\n?”

i.e. ResultString = Regex.Match(strTest, "^(?:(?!Michel).)*$\r?\n?",RegexOptions.Multiline).Value

strTest contains the Test string.

and the job is done.

Happy coding…

No comments: