开发者

How can I capture this through a regex?

开发者 https://www.devze.com 2023-04-06 07:57 出处:网络
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. Th开发者_Python百科e first set of three letters can be ABC

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. Th开发者_Python百科e 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?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号