-1

I have a regex pattern like this :

(?:(?:(?:1[0-2]|0?[1-9])) *[ /.-] *(?:3[01]|[12][0-9]|0?[1-9]) *[ /.-] *(?:19\d{2}|20\d{2}|1[6-9]|2[0-9]))

I want to match it with this text :

001-001 09/23/2019 heure :42

What I am getting matched is:

01 09/23

What I want to have as match :

01 09/23 AND 09/23/2019

My code is actually :

regex = re.compile(pattern)
matches = regex.findall(text)

Is there any option I should turn on ? Thanks

LCMa
  • 445
  • 3
  • 13
  • 1
    You could look into [overlapping matches](https://stackoverflow.com/questions/5616822/python-regex-find-all-overlapping-matches) – The fourth bird Mar 15 '21 at 17:20
  • Overlapping matches, yes, but what are the rules? It is not quite clear why the expected result is not, say, `'01 09/23', '1 09/23', '09/23/2019', '9/23/2019']` – Wiktor Stribiżew Mar 15 '21 at 17:31
  • Does https://ideone.com/nHFWQl work as intended? Note I made days and months require a leading zero and applied the overlapping matches trick. – Wiktor Stribiżew Mar 15 '21 at 17:53

1 Answers1

-1

As @The fourth bird suggest I looked to overlapping matches, and it worked with:

import regex as re
matches = re.findall(pattern, text, overlapped=True)
LCMa
  • 445
  • 3
  • 13
  • We do not answer duplicates, these questions must be closed. Simply let us know via a comment it is just what you need, and we'll close. – Wiktor Stribiżew Mar 16 '21 at 10:40