sommergyll.software(c#);
"Guide to designing a replacement regular expressions in C#"
 

String Replacement

With Regex Replace we can replace strings in the same manner as with the String Replace API. In addition, we can replace strings with the power of the Regular Expression! For instance, we can match strings and substitute them to different places.

To show some of the power of the Regex Replace, we will in the example below convert a US format date to two other formats.The Match and Replace patterns are fairly long and contains a new Grouping construct, but we will progress slowly.

using System.Text.RegularExpressions;
string pattern = @"(?<mm>\d{2}).(?<dd>\d{2}).(?<yyyy>\d{4})";
string source = @"05/13/2006";
string replaceStr = @"${yyyy}-${mm}-${dd} or ${dd}.${mm}.${yyyy}";

// GetOptions is a user defined method that collects
// and combines the option values from Checkboxes in a Form
RegexOptions optionsVal = GetOptions();

string modifiedString =
     Regex.Replace(source, pattern, replaceStr, optionsVal);               

The snippet above shows the C# code necessary to replace some text by using Regular Expressions. We used the static version of the Replace API but it is also available as an instance method.

The RegexOptions are flags that can be combined to change the behaviour of the API: e.g. we can set the IgnoreCase, Multiline or Singleline flags. The IgnoreCase does what its name says, Multiline changes the meaning of "^" and "$" to match on each line instead of only the beginning and end of the whole string. Singleline changes the meaning of "." so it will match any character (default is to not match "\n").

Our Pattern String

(?<mm>\d{2}).(?<dd>\d{2}).(?<yyyy>\d{4})
0123456789012345678901234567890123456789
          1         2         3         

If you have read through the previous section, you will recognise the Capture item between parentheses on index 0 - 11. Here, we have named the Capture item to "mm" by using the "?<mm>" naming syntax.

 

Similarly, we have the named Capture items "dd" and "yyyy" on index 14 and 27 respectively. Note that the names can be ones of your own choice, and even numbers, but as with all programming, we choose some that reflect their purposes.

The "\d{2}" pattern on index 6 and 19 means that we match any two digits, representing the month and day, and as suspected, "\d{4}" match any four digits and represents the year.

On index 12 and 25 we have put a "." to match any type of delimiter, such as "/" or "-".

Our Replacement String

${yyyy}-${mm}-${dd} or ${dd}.${mm}.${yyyy}
012345678901234567890123456789012345678901
          1         2         3         4

The Grouping mentioned for the Match Pattern has its peer in the Replacement Pattern. To demonstrate the flexibility of the Replace API, we have put two target dates in this string. Whatever year the match picks up, it will put it in both Capture items named "yyyy", at index 0 and 35 in the modified string. The same will happen for the other two named Capture items. In the Replacement Pattern, the usual quantifier characters ("?", "*" and "+") are not available and if used, they just represent the character itself.

Our Source String

05/13/2006
0123456789

The source string is a trivial US formatted date (month/day/year) which we want to convert to two European formats: year-month-day and day.month.year.

The Result

2006-05-13 or 13.05.2006
012345678901234567890123
          1         2

While not surprising, it shows that by using Regular Expression we can handle some pretty complex strings.

References

Want to find more about Regular Expressions? Please visit:

 
 

Regex Start | Regex Complex | ComboBox control | Front Page

Disclaimer

© Copyright 2003-2010 Sommergyll Software. All Rights Reserved.

Basic Guide on Regular Expressions