Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
54 lines (45 sloc) 2.81 KB
title ms.date ms.technology dev_langs helpviewer_keywords ms.assetid
Regular Expression Example: Changing Date Formats
03/30/2017
dotnet-standard
csharp
vb
searching with regular expressions, examples
parsing text with regular expressions, examples
regular expressions, examples
.NET Framework regular expressions, examples
regular expressions [.NET Framework], examples
pattern-matching with regular expressions, examples
5fcc75a5-09d7-45ae-a4c0-9ad6085ac83d

Regular Expression Example: Changing Date Formats

The following code example uses the xref:System.Text.RegularExpressions.Regex.Replace%2A?displayProperty=nameWithType method to replace dates that have the form mm/dd/yy with dates that have the form dd-mm-yy.

Example

[!code-csharpRegularExpressions.Examples.ChangeDateFormats#1] [!code-vbRegularExpressions.Examples.ChangeDateFormats#1]

The following code shows how the MDYToDMY method can be called in an application.

[!code-csharpRegularExpressions.Examples.ChangeDateFormats#2] [!code-vbRegularExpressions.Examples.ChangeDateFormats#2]

Comments

The regular expression pattern \b(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{2,4})\b is interpreted as shown in the following table.

Pattern Description
\b Begin the match at a word boundary.
(?<month>\d{1,2}) Match one or two decimal digits. This is the month captured group.
/ Match the slash mark.
(?<day>\d{1,2}) Match one or two decimal digits. This is the day captured group.
/ Match the slash mark.
(?<year>\d{2,4}) Match from two to four decimal digits. This is the year captured group.
\b End the match at a word boundary.

The pattern ${day}-${month}-${year} defines the replacement string as shown in the following table.

Pattern Description
$(day) Add the string captured by the day capturing group.
- Add a hyphen.
$(month) Add the string captured by the month capturing group.
- Add a hyphen.
$(year) Add the string captured by the year capturing group.

See also

You can’t perform that action at this time.