Regex Help: Extracting Dates from Noisy Text

Regex Help: Extracting Dates from Noisy Text

Hi everyone,

I’m having a bit of trouble with a regular expression (regex) pattern. I need to extract just the date values from a string like this:

LineItemNum
DTArticleNumber92296
Quantity Ordrd
UOMEA
UPrice441.60
LineItemNo
ConfirmedQuantity1
ConfirmedDelivery
Requested Delivery12/30/2023 Line Item No.DT Article NumberQuantity OrderedUOMUnit PriceRequested DeliveryItem ScheduleConfirmed DeliveryConfirmed Quantity2

My current regex is:

(?P<RequestedDelivery>.*)

This captures everything after “ConfirmedDelivery”, including the desired date and subsequent noise. I only want the actual date, like “12/30/2023”.

I’ve tried using:

  • \d+\/\d+\/\d+ but it doesn’t match dates with dashes (“-”).
  • Lookaround assertions haven’t helped either.

I’m stuck and would really appreciate some help from the regex gurus here! Ideally, I’d like a regex that can handle both date formats like MM/DD/YYYY and DD-MM-YYYY.

Thanks in advance for your assistance!

Hi @divyanshu52

→ Take an assign activity and create a string variable and store the input data.
→ Use the below regular expression to extract the required date.

- Assign -> output = System.Text.Regularexpressions.Regex.Match(Input,"\d+\/\d+\/\d+|\d+\-\d+\-\d+")

Check the below image for better understanding,

It will extract both formats in MM/DD/YYYY and DD-MM-YYYY

Hope it helps!!

Hi @divyanshu52

Try below

(?<=Requested Delivery).*(\d+\/\d+\d+)

Hi @divyanshu52

Please give the below syntax in the assign activity

(?<=ConfirmedDelivery[\s\S]*?)(\d+(\/|\-)\d+(\/|\-)\d+)

@divyanshu52

It will work in both scenarios please refer this


(?<=Requested Delivery)(\d{2}/\d{2}/\d{4})
U can try the above regex pattern

Hi @divyanshu52
Please try this:
(?<!\d)(\d{1,2}[-\/]\d{1,2}[-\/]\d{4})(?!\d)

In UiPath:
System.Text.RegularExpressions.Regex.Match(str, "(?<!\d)\d{1,2}[-\/]\d{1,2}[-\/]\d{4}(?!\d)").ToString

An optimistic quick dirty pattern:

Hi @divyanshu52

Use below Regex Expression, It’ll handle both the format MM/DD/YYY or DD-MM-YYYY

System.Text.RegularExpressions.Regex.Match(Str_Variable, “Requested Delivery\s*(\d{1,2}[-/]\d{1,2}[-/]\d{2,4})”).Groups(1).Value

Hope it will helps you :slight_smile:
Cheers!!

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