Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incorrect answer to regex practice problem hpa-day-p1 #170

Open
bnmnetp opened this issue Feb 18, 2025 · 0 comments
Open

incorrect answer to regex practice problem hpa-day-p1 #170

bnmnetp opened this issue Feb 18, 2025 · 0 comments

Comments

@bnmnetp
Copy link
Contributor

bnmnetp commented Feb 18, 2025

A user reported this on the discord.

This is the first of the advanced microparsons practice problems for regex.
\d{2}/\d{2}/(\d{2}|\d{4}) should be \d{2}/\d{2}/(\d{4}|\d{2})

If the longer \d{4} is not first then it will always match with the \d{2} and will return the first two digits of the year for example 20 of 2025 if the full four digit year is provided.

In [1]: import re

In [2]: re.match(r'\d{2}/\d{2}/(\d{2}|\d{4})', '12/22/2024')
Out[2]: <re.Match object; span=(0, 8), match='12/22/20'>

In [3]: re.match(r'\d{2}/\d{2}/(\d{2}|\d{4})', '12/22/24')
Out[3]: <re.Match object; span=(0, 8), match='12/22/24'>

In [4]: re.match(r'\d{2}/\d{2}/(\d{4}|\d{2})', '12/22/24')
Out[4]: <re.Match object; span=(0, 8), match='12/22/24'>

In [5]: re.match(r'\d{2}/\d{2}/(\d{4}|\d{2})', '12/22/2024')
Out[5]: <re.Match object; span=(0, 10), match='12/22/2024'>

In [6]: re.match(r'\d{2}/\d{2}/(\d{2}|\d{4})', '12/22/2024').groups()
Out[6]: ('20',)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant