Back to Devexpress

How to: Use Regular Expressions to Convert Date Formats

windowsforms-7982-controls-and-libraries-rich-text-editor-examples-search-and-replace-how-to-use-regular-expressions-to-convert-date-formats.md

latest2.1 KB
Original Source

How to: Use Regular Expressions to Convert Date Formats

  • Dec 12, 2022

The RichEdit Search and Replace functionality supports 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:

After replacement:

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

csharp
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));
vb
document.AppendText("12\14\2014" & Environment.NewLine)
Dim pattern As String = "(?<mm>\d{2}).(?<dd>\d{2}).(?<yyyy>\d{4})"
Dim replacementString As String = "${yyyy}-${mm}-${dd} or ${dd}.${mm}.${yyyy}"
Dim myRegEx As New System.Text.RegularExpressions.Regex(pattern)
Dim count As Integer = document.ReplaceAll(myRegEx, replacementString)
System.Windows.Forms.MessageBox.Show(String.Format("We've done {0} replacement(s).", count))