Working with Regular Expressions in C# - Working with RegExes
(Page 2 of 5 )
Decades ago people in general were getting familiar with the wildcards in the MS-DOS operating system. It wasn’t unusual to see their use pretty frequently in everyday commands such as: “copy *.exe a:” and so forth. This equaled copying all of the executable files (ending in .exe format) to the a: floppy disk drive. Now, regular expressions are much like these nifty wildcards, but they are a lot more powerful.
You have a lot of flexibility when using regular expressions because you can format practically any sort of pattern if you are familiar with it and know its exact and correct syntax. Surely, you may say that glancing over the table of syntax isn’t that hard; that’s true, but that doesn’t guarantee that you will also learn the little details of their usage. In this article we’re going to use the .NET framework’s RegEx processor in C#.
Nowadays, working with the regular expressions in .NET languages has been made easy because the assembly is merged into the main framework. Therefore, all you need to do is include the necessary System.Text.RegularExpressions namespace with the help of the “using” statement at the beginning of your application.
There are a couple of ways to work with regular expressions. Deciding on which route to follow depends only on what you want to accomplish using RegExes. For example, you can use them to check the existence of a specific pattern in a particular source string, or you may want to search and store the matches in a collection, or who knows, you may want to validate the syntax of an e-mail address (anything@blah.net).
As a result, you not only need to know the syntax of regular expressions to “talk” with the RegEx processor, you also want to know the pre-existing classes in the said namespace. Chances are, you may face situations where you need to code a bit “more” than just matching strings and all that. Surely, memorizing the table of RegExes helps in the case of UNIX system administrators… grep does wonders, you know.
Okay, I was getting ahead of myself. Let’s get down to work. In this section we’ll create some basic applications that work with Regular Expressions. You will find C# code snippets here. And in the next section I’ll present to you the periodic table of the RegEx syntax; it’s not really periodic… but it looks quite cryptic for beginners at first glance.
First things first— there’s the Regex.IsMatch() method. It has three parameters: string input, string pattern, and RegexOptions options. This method indicates whether the specified pattern can be found inside the source string. It returns a plain boolean (true or false). The latter parameter is optional but it may be useful in such cases where ignoring the case is required; this can be done using the following RegexOption:
System.Text.RegularExpressions.RegexOptions.IgnoreCase
Let’s enumerate a few of the RegexOptions:
IgnorePatternWhitespace – removes un-escaped white spaces.
IgnoreCase – ignores the case of the input string.
CultureInvariant – ignores the culture of the string.
Singleline – changes the mode to single-line.
RightToLeft – the input string is read from right to left; this is great for some languages.
Multiline – changes the mode to multi-line (^ and $ will apply for lines).
Next: Working with RegExes, continued >>
More C# Articles
More By Barzan "Tony" Antal