python - How can I match the whole regex not the subexpression -
say, have following regex search series of room number:
import re re.findall(r'\b(\d)\d\1\b','101 102 103 201 202 203') i want search room number first , last digit same (101 , 202). above code gives
['1','2'] which corresponding subexpression (\d). how can return whole room number 101 , 202?
import re print [i i,j in re.findall(r'\b((\d)\d\2)\b','101 102 103 201 202 203')] or
print [i[0] in re.findall(r'\b((\d)\d\2)\b','101 102 103 201 202 203')] you can use list comprehension here.you need room numbers include i.basically re.findall return groups in regex.so need 2 groups.the first have room numbers , second used matching.so can extract first out of tuple of 2.
Comments
Post a Comment