andrew.paul.acosta
andrew.paul.acosta

Reputation: 7

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.

Upvotes: 1

Views: 49

Answers (2)

alecxe
alecxe

Reputation: 473893

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

Upvotes: 2

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.

Upvotes: 0

Related Questions