regex - Is there a way in Python to create a tuple of the components matched in a regular expression? -


is there way in python create tuple of components matched in regular expression? instance, trying do.

import re pattern = '^[a-z]{5} [0-9]{6}(c|p)[0-9]{1,3}$' str = 'abcde 020816c110'  m = re.match(pattern,str) print m.group() abcde 020816c110 

i want make looks ('abcde','020816','c','110') (based upon parts within regex) , if pattern different, say,

pattern = ^[a-z]{1,4} [a-z]{2} [a-z]$ str = 'abc fh p' 

i ('abc','fh','p') seems have split on components of regex different pattern.

i considering making n number of separate calls re.search component pattern, doubt find appropriate substring or return more want.

use capturing groups:

>>> pattern = '^([a-z]{5}) ([0-9]{6})(c|p)([0-9]{1,3})$' >>> m = re.match(pattern, str) >>> m.groups() ('abcde', '020816', 'c', '110') 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -