andrew.paul.acosta February 2016

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

Is there a way in Python to create a tuple of the components matched in a regular expression? For instance, this is what I am trying to 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 to make something that looks like ('ABCDE','020816','C','110') (based upon the parts within the regex) and if my pattern is different, say,

pattern = ^[A-Z]{1,4} [A-Z]{2} [A-Z]$
str = 'ABC FH P'

I would eventually get ('ABC','FH','P') It seems I have to split on components of the regex that will be different by pattern.

I am considering making n number of separate calls to re.search with only the component pattern, but I doubt I will always find the appropriate substring or it will return more than I want.

Answers


alecxe February 2016

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')


Martín Muñoz del R&# February 2016

Try:

>>> import re
>>> pattern = '^([A-Z]{5}) ([0-9]{6})(C|P)([0-9]{1,3})$'
>>> s =  'ABCDE 020816C110'
>>> m = re.match(pattern, s)
>>> m.groups()
('ABCDE', '020816', 'C', '110')

You can use groups, and match, for this you only have to add ( and ) in the correct places.

Post Status

Asked in February 2016
Viewed 2,582 times
Voted 5
Answered 2 times

Search




Leave an answer