0

I am tying to write a regular expression to match the dates in a string.

timeStamps = '25-12-2017 25:12:2017 25.12.2017'

pattern = r'(\d{2}(?:-|:|.)){2}\d{4}'

[ re.search(pattern,ts) for ts in timeStamps.split()]

re.search is working without any problem and works exactly the way wanted.

[<_sre.SRE_Match object; span=(0, 10), match='25-12-2017'>,
 <_sre.SRE_Match object; span=(0, 10), match='25:12:2017'>,
 <_sre.SRE_Match object; span=(0, 10), match='25.12.2017'>]

When i change the re.search to re.findall, The result was different.

[ re.findall(pattern,ts) for ts in timeStamps.split()]
[['12-'], ['12:'], ['12.']]

Why the two functions are giving two different output?

Fuji Komalan
  • 1,979
  • 16
  • 25

0 Answers0