0

I am aware there are numerous answers on SO about getting a date regex, but I am trying to create something readable in python.

I have come up with the following so far. Can someone review the below code?

def getDateRegex():
   delim = r'[/\.\- ]'
   dd = r'[0-3]*[0-9](st|th|nd){0,1}' # get a better 01-31 regex
   mm = r'[0-1]*[0-9]' # get a better 01-12 regex
   yyORyyyy = r'(?:[0-9]{2}){1,2}'
   month = r'(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)'
   mmORmonth = r'({0}|{1})'.format(mm,month)
   dd_mmORmonth_yyORyyyy = delim.join([dd, mmORmonth, yyORyyyy]) # format1
   yyORyyyy_mmORmonth_dd = delim.join([yyORyyyy, mmORmonth, dd])  # format2
   final_regex = r'({0}|{1})'.format(dd_mmORmonth_yyORyyyy,yyORyyyy_mmORmonth_dd)
   return final_regex
James Z
  • 12,209
  • 10
  • 24
  • 44
Amarsh
  • 11,214
  • 18
  • 53
  • 78
  • 1
    What would really help from your side would be some sample input (for different cases) and expected output. – cs95 Oct 17 '17 at 00:39
  • cant think of all possible test cases, but https://regexr.com/39tr1 from https://stackoverflow.com/questions/15491894/regex-to-validate-date-format-dd-mm-yyyy is a good sample. guess the community can add more to it – Amarsh Oct 17 '17 at 01:00
  • 1
    probably want (st|th|nd) for 22nd and so forth – information_interchange Oct 17 '17 at 01:31
  • 1
    Right now you're allowing different delimiters in the same date, something like 1990/12-24. That's probably not what you want. – Knoep Oct 17 '17 at 01:31
  • thanks @information_interchange. done – Amarsh Oct 17 '17 at 03:27
  • @Knoep : i cant think of a way ... can you? – Amarsh Oct 17 '17 at 03:28
  • You can put the delimiter in a group and later refer to it, something like this: `…([/\.\- ])…\1…`. `\n` will refer to the content of the nth group in your pattern. – Knoep Oct 17 '17 at 09:50
  • To make it more robust, you can also name the delimiter group and than refer to it by name using the following syntax: `…(?P[/\.\- ])…(?P=delim)…` – Knoep Oct 17 '17 at 09:57
  • "update the needful" is probably an Indian(?) idiom that really makes no sense to others. although even google basically finds only this question. You should try to write clear statements. Tried to fix the question to make more sense. – James Z Oct 17 '17 at 16:45

0 Answers0