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

Popular posts from this blog

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

android - How to create dynamically Fragment pager adapter -

1111. appearing after print sequence - php -