Say I got a string that looks like this D1011608201313
The first part is random letters and the medium part is a date formatted like dd/mm/yyyy and the ladder an id for a record. However the first part can be pretty much random
Like [Random String][DateTime][ID], how would I find the placement of the datetime. The length of the random string, is about 4 to 8 characters.
If I can find the datetime, it should be pretty straight forward from there :)
You could use this RegEx supposing the date is in DDMMYYYY format, and the dates are in the year range of 1900-2099, but there will be some probability for ambiguity. I also updated this to be based off your comment in your question that the dates are from the current month.
public static void Main()
{
// Leaves room for ambiguity if the random prefix or index suffix look
// like dates as well.
var pattern = "((0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])((19|20)[0-9]{2}))";
// Or, I see in your comment that the dates are from the current month.
// If so then this decreases the probability of a false match. You could
// use the following pattern instead:
var year = DateTime.Today.Year;
var month = string.Format("{0:00}", DateTime.Today.Month);
pattern = "((0[1-9]|[12][0-9]|3[01])(" + month + ")(" + year + "))";
var str = "D1011608201313";
var matches = Regex.Matches(str, pattern);
if (matches.Count == 0) return;
var groups = matches[0].Groups;
int d, m, y;
int.TryParse(groups[2].Value, out d);
int.TryParse(groups[3].Value, out m);
int.TryParse(groups[4].Value, out y);
var date = new DateTime(y, m, d);
Console.WriteLine(date);
}
Detailed breakdown of the RegEx (from RegexBuddy):
((0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])((19|20)[0-9]{2}))
Match the regular expression below and capture its match into backreference number 1 «((0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])((19|20)[0-9]{2}))»
Match the regular expression below and capture its match into backreference number 2 «(0[1-9]|[12][0-9]|3[01])»
Match either the regular expression below (attempting the next alternative only if this one fails) «0[1-9]»
Match the character “0” literally «0»
Match a single character in the range between “1” and “9” «[1-9]»
Or match regular expression number 2 below (attempting the next alternative only if this one fails) «[12][0-9]»
Match a single character present in the list “12” «[12]»
Match a single character in the range between “0” and “9” «[0-9]»
Or match regular expression number 3 below (the entire group fails if this one fails to match) «3[01]»
Match the character “3” literally «3»
Match a single character present in the list “01” «[01]»
Match the regular expression below and capture its match into backreference number 3 «(0[1-9]|1[012])»
Match either the regular expression below (attempting the next alternative only if this one fails) «0[1-9]»
Match the character “0” literally «0»
Match a single character in the range between “1” and “9” «[1-9]»
Or match regular expression number 2 below (the entire group fails if this one fails to match) «1[012]»
Match the character “1” literally «1»
Match a single character present in the list “012” «[012]»
Match the regular expression below and capture its match into backreference number 4 «((19|20)[0-9]{2})»
Match the regular expression below and capture its match into backreference number 5 «(19|20)»
Match either the regular expression below (attempting the next alternative only if this one fails) «19»
Match the characters “19” literally «19»
Or match regular expression number 2 below (the entire group fails if this one fails to match) «20»
Match the characters “20” literally «20»
Match a single character in the range between “0” and “9” «[0-9]{2}»
Exactly 2 times «{2}»
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With