O'Reilly logo

C# 7 and .NET Core Cookbook by Dirk Strauss

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

Let's step through the code to understand what is happening. We need to get a regex that looks like this: b(wordToMatch1|wordToMatch2|wordToMatch3)b.

What this basically says is "find me any of the words and only whole words which are denoted by b". When we look at the list we created, we will see the words we want to remove from the input string:

We then created a simple loop that will create the list of words to match using the OR metacharacter. We ended up with a BadWord1|BadWord2|BadWord3 pattern after the foreach loop has completed. However, this is still not a valid regex:

To complete the pattern resulting in the valid ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required