2010-05-12

.NET idiomatic code snippets for regular expressions

Using regular expressions in .NET is far more verbose than in Perl and the examples on MSDN are not very good.

It is also difficult to find idiomatic code for common things on Stack Overflow. Searching will come up with how to make the regular expressions themselves and not how to implement it in terms of C# or VB.NET.

Here is the idomatic code, in C#

1. Simple to match and extract:

string estr = Regex.Match(inputText, @"/(\d+)/").Groups[1].Value;

Where inputText contains the text to be matched and "/(\d+)/" is a sample regular expression. Note that those are forward slashes and are matched literally. What is inside the parenthesis pair is extracted, in this case "\d+" (one or more digits - effectively an integer number). Namespace System.Text.RegularExpressions must be included for this to work ("using System.Text.RegularExpressions;").

2. Replace matched text with some fixed text (empty in this case, effectively removing the matched text):

Regex rgx = new Regex(@"\D");
IDstr = rgx.Replace(inputText, "");

Where inputText contains the text to be matched and "\D" is a sample regular expression (matching everything but digits).




NB: The answers to Stack Overflow question "Regular expression to retrieve everything before first slash" and "Parse filename from full path using regular expressions in C#" does contain some sample code.

No comments:

Post a Comment