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!
mkankatala
(Mahesh Kankatala)
January 11, 2024, 7:07am
2
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!!
lrtetala
(Lakshman Reddy)
January 11, 2024, 7:07am
3
Hi @divyanshu52
Try below
(?<=Requested Delivery).*(\d+\/\d+\d+)
vrdabberu
(Varunraj Dabberu)
January 11, 2024, 7:07am
4
Hi @divyanshu52
Please give the below syntax in the assign activity
(?<=ConfirmedDelivery[\s\S]*?)(\d+(\/|\-)\d+(\/|\-)\d+)
rlgandu
(Rajyalakshmi Gandu)
January 11, 2024, 7:11am
5
@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
pikorpa
(Piotr Kołakowski)
January 11, 2024, 8:06am
7
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
ppr
(Peter Preuss)
January 11, 2024, 8:41am
8
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
Cheers!!
system
(system)
Closed
January 18, 2024, 9:03am
10
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.