Regex to get date time from string

09/05/2023 8:15
Reason:Driver Related/Driver Error
or
09/05/2023 21:15
Reason:Driver Related/Driver Error

Need regex to get date and time from the above scernaios(text highlighed in bold).

Hi @veeramani.shanmugam

You can use the following regular expression to extract the date from the provided scenarios:

\b\d{2}/\d{2}/\d{4}\b

This regular expression will match the date in the format dd/mm/yyyy, where:

  • \b matches a word boundary to ensure it’s a whole word.
  • \d{2} matches two digits for the day.
  • / matches the forward slash character.
  • \d{2} matches two digits for the month.
  • / matches another forward slash character.
  • \d{4} matches four digits for the year.

You can use this regular expression in your code to extract the date from the scenarios provided.

Thanks!!

Thanks for your quick reply.

Need date and time

Try this-

To extract the date and time from the provided scenarios, you can use the following regular expression:

\d{2}/\d{2}/\d{4} \d{1,2}:\d{2}

This regular expression matches the date in the format dd/mm/yyyy followed by a space and then the time in the format h:mm. It can handle both scenarios you’ve provided:

  • \d{2} matches two digits for the day.
  • / matches the forward slash character.
  • \d{2} matches two digits for the month.
  • / matches another forward slash character.
  • \d{4} matches four digits for the year.
  • (space) matches the space between date and time.
  • \d{1,2} matches one or two digits for the hour.
  • : matches the colon between hours and minutes.
  • \d{2} matches two digits for the minutes.

This regular expression will match both of the scenarios you provided and extract the date and time.

Thanks!!

Yes, working as expected. Thank you

1 Like

hi,@veeramani.shanmugam

System.Text.RegularExpressions.Regex.Match(str,“\d+.+”).Value
image

hope it helps!!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.