-1

I am trying to extract the date from this '2025-03-21T12:54:41Z' text using python regular expression.

date=re.match('(\d{4})[/.-](\d{2})[/.-](\d{2})$', date[0])
print(date)

This give output as None

also, I tried this code

date_reg_exp = re.compile('\d{4}(?P<sep>[-/])\d{2}(?P=sep)\d{2}')
matches_list=date_reg_exp.findall(date[0])
for match in matches_list:
  print match

This gives output as - only

Please help

Ujjwal Pawar
  • 31
  • 2
  • 6

2 Answers2

0

Your regular expression is wrong because it has a $ at the end. $ asserts that this is the end of the string.

The regex engine matches your string with the regex and after matching the last two digits, expects a $ - end of the string. However, your string still has T12:54:41Z before the end, so the regex does not match.

To fix this, remove $:

>>> re.match('(\d{4})[/.-](\d{2})[/.-](\d{2})', '2025-03-21T12:54:41Z')
<_sre.SRE_Match object; span=(0, 10), match='2025-03-21'>
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

Instead of using $ sigil at the end of your regexp, which is matching end-of-line character, try using ^ at the beginning:

import re
date='2025-03-21T12:54:41Z'
date=re.match('^(\d{4})[/.-](\d{2})[/.-](\d{2})', date)
print(date)

Output in python3:

<_sre.SRE_Match object; span=(0, 10), match='2025-03-21'>

Python2:

<_sre.SRE_Match object at 0x7fd191ac1ae0>
yahol
  • 23
  • 5