Reputation: 521
Is there a more efficient way of joining a tuple? since my rx gives me a tuple? Moreover c is '7:30' ' AM' at the end, and i need '7:30 AM'
import re
rx = r"(?i)\b(\d{1,2}:\d{2})(?:-\d{1,2}:\d{2})?(\s*[pa]m)\b"
s = "ankkjdf 7:30-8:30 AM dds "
matches = re.findall(rx, s)
m=str(matches)
a =''.join(m[2:8])
b= ''.join(m[9:15])
c = "".join(a + b)
print(c)
Upvotes: 2
Views: 70
Reputation: 8510
>>> import re
>>> rx = r"(?i)\b(\d{1,2}:\d{2})(?:-\d{1,2}:\d{2})?(\s*[pa]m)\b"
>>> s = "ankkjdf 7:30-8:30 AM dds "
>>> matches = re.findall(rx, s)
>>> matches
[('7:30', ' AM')]
>>> [ "".join(x) for x in matches]
['7:30 AM']
>>>
or
>>> "".join(matches[0])
'7:30 AM'
>>>
or directly from the source
>>> [ "".join(x) for x in re.findall(rx, s)]
['7:30 AM']
>>> "".join( re.findall(rx, s)[0] )
'7:30 AM'
>>>
there is no reason to do m=str(matches)
, just fuse together what you get in whatever way you want...
with the latest example
>>> test="Join us for a guided tour of the Campus given by Admissions staff. The tour will take place from 1:15-2:00 PM EST and leaves from the Admissions Office."
>>> [ "".join(x) for x in re.findall(rx, test)]
['1:15 PM']
>>> "".join( re.findall(rx, test)[0] )
'1:15 PM'
>>>
Upvotes: 2