How to: Use Regular Expressions to Convert Date Formats

The XtraRichEdit Search and Replace functionality supports using Regular Expressions. This example illustrates how you can benefit from using them.

You can change US formatted dates in the document to the European format.

User Interface

This task can be accomplished via the end-user form, as shown in the pictures below.

Before replacement:

Regex_Email_Before

After replacement:

Regex_Email_After

API

The following code snippet illustrates how the SubDocument.ReplaceAll method with Regular Expressions can be used to convert all US formatted dates (month/day/year) in the document to two European formats: year-month-day and day.month.year.

View Example

document.AppendText("12\\14\\2014" + Environment.NewLine);
string pattern = @"(?<mm>\d{2}).(?<dd>\d{2}).(?<yyyy>\d{4})";
string replacementString = @"${yyyy}-${mm}-${dd} or ${dd}.${mm}.${yyyy}";
System.Text.RegularExpressions.Regex myRegEx =
    new System.Text.RegularExpressions.Regex(pattern);
int count = document.ReplaceAll(myRegEx, replacementString);
System.Windows.Forms.MessageBox.Show(String.Format("We've done {0} replacement(s).", count));