Exemple d'expression régulière : modification des formats de date

L’exemple de code suivant utilise la méthode Regex.Replace pour remplacer les dates au format mm/jj/aa par des dates au format jj-mm-aa.

Avertissement

Lorsque vous utilisez System.Text.RegularExpressions pour traiter une entrée non approuvée, passez un délai d’expiration. Un utilisateur malveillant peut fournir une entrée à RegularExpressions, provoquant une attaque par déni de service. Les API d’infrastructure ASP.NET Core qui utilisent RegularExpressions passent un délai d’expiration.

Exemple

static string MDYToDMY(string input)
{
   try {
      return Regex.Replace(input,
             @"\b(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{2,4})\b",
            "${day}-${month}-${year}", RegexOptions.None,
            TimeSpan.FromMilliseconds(150));
   }
   catch (RegexMatchTimeoutException) {
      return input;
   }
}
Function MDYToDMY(input As String) As String
    Try
        Return Regex.Replace(input, _
               "\b(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{2,4})\b", _
               "${day}-${month}-${year}", RegexOptions.None,
               TimeSpan.FromMilliseconds(150))
    Catch e As RegexMatchTimeoutException
        Return input
    End Try
End Function

Le code suivant montre comment la méthode MDYToDMY peut être appelée dans une application.

using System;
using System.Globalization;
using System.Text.RegularExpressions;

public class Class1
{
   public static void Main()
   {
      string dateString = DateTime.Today.ToString("d",
                                        DateTimeFormatInfo.InvariantInfo);
      string resultString = MDYToDMY(dateString);
      Console.WriteLine("Converted {0} to {1}.", dateString, resultString);
   }

   static string MDYToDMY(string input)
   {
      try {
         return Regex.Replace(input,
                @"\b(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{2,4})\b",
               "${day}-${month}-${year}", RegexOptions.None,
               TimeSpan.FromMilliseconds(150));
      }
      catch (RegexMatchTimeoutException) {
         return input;
      }
   }
}
// The example displays the following output to the console if run on 8/21/2007:
//      Converted 08/21/2007 to 21-08-2007.
Imports System.Globalization
Imports System.Text.RegularExpressions

Module DateFormatReplacement
    Public Sub Main()
        Dim dateString As String = Date.Today.ToString("d", _
                                             DateTimeFormatInfo.InvariantInfo)
        Dim resultString As String = MDYToDMY(dateString)
        Console.WriteLine("Converted {0} to {1}.", dateString, resultString)
    End Sub

    Function MDYToDMY(input As String) As String
        Try
            Return Regex.Replace(input, _
                   "\b(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{2,4})\b", _
                   "${day}-${month}-${year}", RegexOptions.None,
                   TimeSpan.FromMilliseconds(150))
        Catch e As RegexMatchTimeoutException
            Return input
        End Try
    End Function
End Module
' The example displays the following output to the console if run on 8/21/2007:
'      Converted 08/21/2007 to 21-08-2007.

Commentaires

Le modèle d'expression régulière \b(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{2,4})\b est interprété comme indiqué dans le tableau suivant.

Modèle Description
\b Commencer la correspondance à la limite d'un mot.
(?<month>\d{1,2}) Mettre en correspondance un ou deux chiffres décimaux. Il s’agit du groupe capturé month.
/ Mettre en correspondance la barre oblique.
(?<day>\d{1,2}) Mettre en correspondance un ou deux chiffres décimaux. Il s’agit du groupe capturé day.
/ Mettre en correspondance la barre oblique.
(?<year>\d{2,4}) Mettre en correspondance de deux à quatre chiffres décimaux. Il s’agit du groupe capturé year.
\b Terminer la correspondance à la limite d'un mot.

Le modèle ${day}-${month}-${year} définit la chaîne de remplacement comme indiqué dans le tableau suivant.

Modèle Description
$(day) Ajouter la chaîne capturée par le groupe de capture day.
- Ajouter un trait d’union.
$(month) Ajouter la chaîne capturée par le groupe de capture month.
- Ajouter un trait d’union.
$(year) Ajouter la chaîne capturée par le groupe de capture year.

Voir aussi