fork download
  1. import re
  2.  
  3. txt1 = "The date is 3-Dec "
  4. txt2 = "The date is3-Dec "
  5. txt3 = "The date is 3-Dec"
  6. txt4 = "The date is Dec-20"
  7. txt5 = "The date isDec-20"
  8.  
  9. months = ['Nov', 'Dec']
  10. date_rx = r'(?:[1-9]|[12]\d|3[01])'
  11. month_rx = r'(?:{})'.format('|'.join(months))
  12. sep_rx = r'[\s,/\\-]'
  13. regx = r"(?<!\d){0}{2}{1}|{1}{2}{0}(?!\d)".format(date_rx, month_rx, sep_rx)
  14. print(regx)
  15.  
  16. x1 = re.findall(regx, txt1)
  17. print(x1)
  18. x2 = re.findall(regx, txt2)
  19. print(x2)
  20. x3 = re.findall(regx, txt3)
  21. print(x3) # empty
  22. x4 = re.findall(regx, txt4)
  23. print(x4)
  24. x5 = re.findall(regx, txt5)
  25. print(x5) # empty
Success #stdin #stdout 0.02s 9672KB
stdin
Standard input is empty
stdout
(?<!\d)(?:[1-9]|[12]\d|3[01])[\s,/\\-](?:Nov|Dec)|(?:Nov|Dec)[\s,/\\-](?:[1-9]|[12]\d|3[01])(?!\d)
['3-Dec']
['3-Dec']
['3-Dec']
['Dec-20']
['Dec-20']