0

im trying to do this (Write a regular expression that can detect dates in the DD/MM/YYYY format. Assume that the days range from 01 to 31, the months range from 01 to 12, and the years range from 1000 to 2999. Note that if the day or month is a single digit, it’ll have a leading zero. The regular expression doesn’t have to detect correct days for each month or for leap years; it will accept nonexistent dates like 31/02/2020 or 31/04/2021.)

the solution is giving me 4 values in a tuple (date, day, month, year) instead of just (date) how do i fix this? thanks in advance.

import re

dates_regex = re.compile(r'''(

#day
(0[1-9]|[12][0-9]|3[01])

/

(0[1-9]|[10][12])

/

([1-2][0-9][0-9][0-9])



)''', re.VERBOSE)

string = """ 05/06/400
06/05/2031
29/04/5000
08/07/2164
01/12/1832
27/09/6000
08/01/1810
16/05/1503
35/12/2000
21/09/1915"""

regex_search = dates_regex.findall(string)

print(regex_search)

running the code results in:

[('06/05/2031', '06', '05', '2031'), ('08/07/2164', '08', '07', '2164'), ('01/12/1832', '01', '12', '1832'), ('08/01/1810', '08', '01', '1810'), ('16/05/1503', '16', '05', '1503'), ('21/09/1915', '21', '09', '1915')]

1 Answers1

0

this code returns what you need

import re

dates_regex = re.compile(r'''(

#day
(0[1-9]|[12][0-9]|3[01])

(/)

(0[1-9]|[10][12])

(/)

([1-2][0-9][0-9][0-9])



)''', re.VERBOSE)

string = """ 05/06/400
06/05/2031
29/04/5000
08/07/2164
01/12/1832
27/09/6000
08/01/1810
16/05/1503
35/12/2000
21/09/1915"""

regex_search = dates_regex.finditer(string)

for i in regex_search:
    print(i.group(0))
Dmitriy Neledva
  • 867
  • 4
  • 10
  • and about your question 'why there is 4 values instead of 1' it's because you use unnecessary parentheses. instead of this `r'''( (0[1-9]|[12][0-9]|3[01]) / (0[1-9]|[10][12]) / ([1-2][0-9][0-9][0-9]) )'''` try this `r'''(0[1-9]|[12][0-9]|3[01]) / (0[1-9]|[10][12]) / ([1-2][0-9][0-9][0-9])'''` – Dmitriy Neledva Sep 24 '22 at 19:47
  • What's are the parentheses around the slashes `(/)` for? It works exactly the same without those... – Ignatius Reilly Sep 24 '22 at 23:41
  • What's the reasoning for your changes to the original code?? Yes, it works... but it would work the same using `findall` as in the original code, and then `i[0]` in your loop instead of `i.group(0)`. I don't think the OP are asking how to get the first element of tuples inside a list, they probably know already how to do it! They're asking how to get the whole target string in a single capturing group. And the obvious answer -"don't capture the groups you are not interested in"- was already given in comments. – Ignatius Reilly Sep 24 '22 at 23:52
  • I agree with you. the question was 'why there is 4 values instead of 1', the right answer ' there is a couple of spear parentheses in the search pattern'. and what is to be used further finditer or findall it doesn't matter. My initial answer is wrong. – Dmitriy Neledva Sep 25 '22 at 17:53