asp.net.ph

Skip Navigation LinksGetting Started > ASP.NET Base Types > Regular Expressions > Regular Expression Examples > Example: Changing Date Formats

Example: Changing Date Formats

ASP.NET Base Types   Regular Expressions   Regular Expression Examples


The following code example uses the Regex.Replace method to replace dates of the form mm/dd/yy with dates of the form dd-mm-yy.

 [ VB ]
   Function MDYToDMY ( input As String ) As String
      Return Regex.Replace ( input, _
         "\b ( ?<month>\d{1,2} ) / ( ?<day>\d{1,2} ) / ( ?<year>\d{2,4} ) \b", _
         "${day}-${month}-${year}" )
   End Function
 [ C# ]
   String MDYToDMY ( String input )
   {
      return Regex.Replace ( input, 
         "\\b ( ?<month>\\d{1,2} ) / ( ?<day>\\d{1,2} ) / ( ?<year>\\d{2,4} ) \\b",
         "${day}-${month}-${year}" );
   }

Regex Replacement Pattern

This example demonstrates the use of named backreferences within the replacement pattern for Regex.Replace. Here, the replacement expression ${day} inserts the substring captured by the group ( ?<day>… ).

The Regex.Replace function is one of several static functions that enable you to use regular expression operations without creating an explicit regular expression object. This is convenient when you do not want to keep the compiled regular expression.



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note