I'm trying to write a regex expression that can be used to find dates in a string that may be preceded (or followed) by spaces, numbers, text, end-of-line, etc. The expression should handle US date formats that are either
1) Month Name Day, Year - i.e. January 10, 2019 OR
2) mm/dd/yy - i.e. 11/30/19
I found this for Month Name, Day Year
(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4}
(thanks to Veverke here Regex to match date like month name day comma and year
and this for mm/dd/yy (and various combinations of m/d/y)
(1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2}
(thanks to Steven Levithan and Jan Goyvaerts here https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch04s04.html
I have tried to combine them like this
((Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4})|((1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2})
and when I search the input string "Paid on 1/1/2019" for "on [regex above]" it does finds the date but not the word "on". The string is found if I just use
(1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2}
Can anyone see what I'm doing wrong?
Edit
I'm using the c# .net code below:
string stringToSearch = "Paid on 1/1/2019";
string searchPattern = @"on ((Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4})|((1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2})";
var match = Regex.Match(stringToSearch, searchPattern, RegexOptions.IgnoreCase);
string foundString;
if (match.Success)
foundString= stringToSearch.Substring(match.Index, match.Length);
For example
string searchPattern = @"on ((Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4})|((1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2})";
stringToSearch = "Paid on Jan 1, 2019";
found = "on Jan 1, 2019" -- worked as expected, found the word "on" and the date
stringToSearch = "Paid on 1/1/2019";
found = "1/1/2019" -- did not work as expected, found the date but did not include the word "on"
If I reverse the pattern
string searchPattern = @"on ((1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2})|((Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4})"";
stringToSearch = "Paid on Jan 1, 2019";
found = "Jan 1, 2019" -- did not work as expected, found the date but did not include the word "on"
stringToSearch = "Paid on 1/1/2019";
found = "on 1/1/2019" -- worked as expected, found the word "on" and the date
Thanks