Regex to get Datetime from a string

Hello All,

need regex to extract datetime from a string.

example string is :Private Residence
Delivered 01/24/2023 01:38 PM
Recvd by: BRENT M
Appointment scheduled for 01/24/2023 08:00 AM - 04:00 PM
Charges: View charges here

but need only 01/24/2023 01:38 PM

i am suing C# language for UiPath Studio.

thanks in advance.

few more input string sample

sample 1: Shipment Canceled 02/01/2023

sample 2: Delivered 01/23/2023 01:33 PM
Recvd by: GARCIA
Appointment scheduled for 01/23/2023 01:33 PM
Charges: View charges here
COD Amount:

.

Hi @sudha456,
Please use below regex to extract the value

\d{2}\/\d{2}\/\d{4} \d{2}:\d{2} (AM|PM)

To get the date value using below syntax in assign activity

strDate=System.Text.RegularExpressions.Regex.Match(yourString,"\d{2}\/\d{2}\/\d{4} \d{2}:\d{2} (AM|PM)").Value

Regards,
Arivu

2 Likes

Hello Arivu96,

thanks for the quick response.
I tried the same but it sthrowing an exception.

i am suing C# langague to build the project. it might be working for vb.net but not for C#.

@sudha456

Please use like this

System.Text.RegularExpressions.Regex.Match("",@"\d{2}/\d{2}/\d{4} \d{2}:\d{2} (AM|PM)").Value

Feel free to mark solution if working else happy to help

Hope this helps

cheers

Hello Anil,

thanks for quick support.

its working fine when we have AM or PM but returing null when input string does not have those

@sudha456

So according to what we did…it matches the expression only when all of those are present…Else it would be null only…Does some dates dont have AM or PM? if do use the below expression

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

cheers

Thanks a lot,

can i get one more regex to get date when input contains only MM/dd/yyyy?

Thanks a lot

@sudha456

This is for date

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

You can actually match all of those 4 patternn together using this

\d{2}/\d{2}/\d{4}( \d{2}:\d{2})*( AM| PM)*

Hope this helps

cheers

its working fine .

thanks a lot

@sudha456

Happy Automation

cheers

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