Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jeremy O'Bryan
16,672 Pointsmatching a specific pattern with Regex in Python
Hi everybody!
Hoping someone can help me with this, I'm having a heck of a time figuring out what I'm doing wrong here. I have a list in which each element is a time and some other text. What I want to do is count the number of occurrences of the specific 'hour'. This is what I have so far:
import re
my_list = ['390809888 09:40 AM' ,'09088988 09:50 AM', '488881888 12:05 PM']
hour = '12'
r = re.compile('\d{1,2}:\d{1,2}')
newlist = list(filter(r.match, my_list))
print(newlist)
matches = [item[0:2] for item in newlist]
#count = matches.count(hour)
print('{} occurrs {} times in the list'.format(hour, matches.count(hour)))
with this list, it should return 1 match but I keep getting an empty list and 0 matches. I'm sure it has something to do with the regex pattern I'm using but I can't figure out how to make it work. Any help is greatly appreciated as always!
2 Answers
Majid Bilal
3,558 PointsI have achieved it somewhat differently and I'm posting the code here:
import re
my_list = ['390809888 09:40 AM', '09088988 12:50 AM', '488881888 12:05 PM']
hour = '12'
r = re.compile(r'' + str(hour) + ':')
newlist = list(filter(r.search, my_list))
print(newlist)
matches = [item for item in newlist]
print('{} occurrs {} times in the list'.format(hour, ''.join(matches).count(str(hour))))
I have changed the regular expression a bit as we want exactly same digits in the 'hour', so I have used the value of hour as a string and joining it with a ':' since I believe hour will always follow a colon. Further I'm using search instead of match since match(regex) = search(^regex), meaning it will always match at the beginning of the string. For checking the count of hour appearing in matches, I have converted matches to a string by using .join and also I'm converting hour to string since I'm searching through a string, if I were to find exact match of entire list element I would have used list without converting it, but since I want to find occurrence of hour within an element or elements of a list, I'm converting it to a string.
Hope that makes sense to you.
Jeremy O'Bryan
16,672 PointsYes! That absolutely makes sense, I never thought to approach it that way - that helps a lot, thanks so much! :)
Majid Bilal
3,558 PointsMajid Bilal
3,558 PointsI have achieved it somewhat differently and I'm posting the code here:
I have changed the regular expression a bit as we want exactly same digits in the 'hour', so I have used the value of hour as a string and joining it with a ':' since I believe hour will always follow a colon. Further I'm using search instead of match since match(regex) = search(^regex), meaning it will always match at the beginning of the string. For checking the count of hour appearing in matches, I have converted matches to a string by using .join and also I'm converting hour to string since I'm searching through a string, if I were to find exact match of entire list element I would have used list without converting it, but since I want to find occurrence of hour within an element or elements of a list, I'm converting it to a string.
Hope that makes sense to you.