hi,
Dispatch Date: 6/1/2017 1:05 PM
Delivery Date: 6/1/2017 1:42 PM
I want a regular expression for extracting the “Delivery date”.
So
So I tried with this one
But not succeed in extracting the date,can any one suggest a better way of extracting by using regular expression.
In my automation, I have two dates like the above dispatch date and delivery date.
“(D)(e)(l)(i)(v)(e)(r)(y)(\s+)(D)(a)(t)(e)(:)(\s+)((?:(?:[0-2]?\d{1})|(?:[3][01]{1})))(?![\d])(/)((?:(?:[0-2]?\d{1})|(?:[3][01]{1})))(?![\d])(/)((?:(?:[0-2]?\d{1})|(?:[3][01]{1})))(?![\d])”
Regards,
Srikanth
Hi,
Using “text” to represent your string containing the dates…
There are a couple of methods you could use
using .Split:
Date.ParseExact(text.Split({"Delivery Date:"},System.StringSplitOptions.None)(1).Split(System.Environment.Newline(0))(0).Trim,"d/M/yyyy h:mm tt",System.Globalization.CultureInfo.InvariantCulture)
using Regex:
Date.ParseExact(System.Text.RegularExpressions.Regex.Match(text,"Delivery Date: [0-9]{1,2}/[0-9]{1,2}/[0-9]{2,4} [0-9]{1,2}:[0-9]{1,2} [A-Z]{2}").Value.Replace("Delivery Date:","").Trim,"d/M/yyyy h:mm tt",System.Globalization.CultureInfo.InvariantCulture)
This was the pattern used: “Delivery Date: [0-9]{1,2}/[0-9]{1,2}/[0-9]{2,4} [0-9]{1,2}:[0-9]{1,2} [A-Z]{2}”
{1,2} says to find 1 to 2 characters of what is in the bracket.
And I used ParseExact to convert the string to a DateTime in the format “d/M/yyyy h:mm tt”, which can be adjusted.
Regards.
Clayton.
2 Likes