Below is regex exercise where you can see how in if iteration below I a strip() method, which is basically reserved for strings, to a function "re.findall(patt,redata)[i].strip()" to generate the proper list I wanted.
This works because you are dealing with the return value of that function which is a string.
>>> patt
'\\s\\d{4}'
>>> redata
'Tue Jan 14 00:43:21 2020::eaximi@gstwyysnbd.gov::1578951801-6-10 Sat Jul 31 15:17:39 1993::rzppg@wgxvhx.com::744121059-5-6 Mon Sep 21 20:22:37 1987::ttwqrf@rpybrct.edu::559243357-6-7 Fri Aug 2 07:15:23 1991::tjyp@mgfyitsks.net::681106523-4-9 Mon Mar 18 19:59:47 2024::dgzxmb@fhyykji.org::1710781187-6-7 '
>>> t
['2020', '1993', '1987', '1991', '2024']
>>> redata
'Tue Jan 14 00:43:21 2020::eaximi@gstwyysnbd.gov::1578951801-6-10 Sat Jul 31 15:17:39 1993::rzppg@wgxvhx.com::744121059-5-6 Mon Sep 21 20:22:37 1987::ttwqrf@rpybrct.edu::559243357-6-7 Fri Aug 2 07:15:23 1991::tjyp@mgfyitsks.net::681106523-4-9 Mon Mar 18 19:59:47 2024::dgzxmb@fhyykji.org::1710781187-6-7 '
>>> patt
'\\s\\d{4}'
>>> t
[]
>>> re.findall(patt,redata)
[' 2020', ' 1993', ' 1987', ' 1991', ' 2024']
>>> t
[]
>>> for i in range(len(re.findall(patt,redata))):
t.append(re.findall(patt,redata)[i].strip())
>>> t
['2020', '1993', '1987', '1991', '2024']
>>>
This works because you are dealing with the return value of that function which is a string.
>>> patt
'\\s\\d{4}'
>>> redata
'Tue Jan 14 00:43:21 2020::eaximi@gstwyysnbd.gov::1578951801-6-10 Sat Jul 31 15:17:39 1993::rzppg@wgxvhx.com::744121059-5-6 Mon Sep 21 20:22:37 1987::ttwqrf@rpybrct.edu::559243357-6-7 Fri Aug 2 07:15:23 1991::tjyp@mgfyitsks.net::681106523-4-9 Mon Mar 18 19:59:47 2024::dgzxmb@fhyykji.org::1710781187-6-7 '
>>> t
['2020', '1993', '1987', '1991', '2024']
>>> redata
'Tue Jan 14 00:43:21 2020::eaximi@gstwyysnbd.gov::1578951801-6-10 Sat Jul 31 15:17:39 1993::rzppg@wgxvhx.com::744121059-5-6 Mon Sep 21 20:22:37 1987::ttwqrf@rpybrct.edu::559243357-6-7 Fri Aug 2 07:15:23 1991::tjyp@mgfyitsks.net::681106523-4-9 Mon Mar 18 19:59:47 2024::dgzxmb@fhyykji.org::1710781187-6-7 '
>>> patt
'\\s\\d{4}'
>>> t
[]
>>> re.findall(patt,redata)
[' 2020', ' 1993', ' 1987', ' 1991', ' 2024']
>>> t
[]
>>> for i in range(len(re.findall(patt,redata))):
t.append(re.findall(patt,redata)[i].strip())
>>> t
['2020', '1993', '1987', '1991', '2024']
>>>
No comments:
Post a Comment