Legend
Legend

Reputation: 116810

How can I capture this through a regex?

I have some text like this:

ABCPQR01 is not at all good
EFHSTU39 is somewhat nicer

and I want to capture the ABC... and EFH... type words. The first set of three letters can be ABC or EFH and the second set of three letters can be PQR or STU. Instead of writing two separate regexes to capture these two text elements, how can I write this as a single re.compile statement? Any suggestions?

Upvotes: 2

Views: 81

Answers (2)

David Mills
David Mills

Reputation: 551

I think this would be a pattern which would work :)

>>> re.compile("^(ABC|EFH)(PQR|STU)\d\d\b")

also you can test it at http://www.regextester.com/index2.html

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798546

>>> re.match('(ABC|EFH)(PQR|STU)', 'ABCPQR01 is not at all good').groups()
('ABC', 'PQR')

Upvotes: 6

Related Questions