Regex, how to match agreed tags?

I use python regex to match dates on forms: 1999-01-01 or 1999.1.1 or 1999-1-1. but not the same as 1999.1-1 or 1999-1.1.ie, the signs between the annual month and the day must be consecutive. For this, I use the following code, which is messy. Is there a better way to do this?

import regex as re
re.fullmatch('(((((19|20)\d\d-(([1-9])|((0[1-9]|1[012])))-(?p)([1-9]|(0[1-9]|[12][0-9]|3[01]))(\.)?))))|' \
    '(((((19|20)\d\d\/(([1-9])|((0[1-9]|1[012])))/(?p)([1-9]|(0[1-9]|[12][0-9]|3[01]))(\.)?))))|'\
    '(((((19|20)\d\d\.(([1-9])|((0[1-9]|1[012])))\.(?p)([1-9]|(0[1-9]|[12][0-9]|3[01]))(\.)?))))','1999.1.1')
+4
source share
3 answers
\b\d+(\W)\d+\1\d+\b

You can use something based on these lines. See Demo.

https://regex101.com/r/nVCBSy/1/

Basically, you capture a group and then see if it is there.

+1
source

you can use

\d{4}([-/.])\d{1,2}\1\d{1,2}

Watch the demo at regex101.com . The key here is to use a backlink.

+2

:

import re

s = '''1999-01-01 or 1999.1.1 or 1999-1-1
but not 1999.1-1 or 1999-1.1'''

matches = re.findall(r'(\d{4})([.-])(\d{1,2})\2(\d{1,2})', s)

dates = []
for match in matches:
    dates.append((match[0], match[2], match[3]))

print(dates)
+1

Source: https://habr.com/ru/post/1691667/


All Articles