1

我想得到像“abcdef@aaaa.com”这样的输出,但它只显示“com”作为匹配的表达式。如何使用 findall 获得完整匹配的表达式?

>>> pat = re.compile('[a-zA-Z0-9][\w\.]{4}[\w\.]*@[a-zA-Z0-9][a-zA-Z0-9]*[.](com|co.in|org|edu)')
>>> pat.findall('abcdef@aaaa.com')
['com']
>>> pat.findall('abcdef@aaaa.com bsdkbsbd@bkdjb.orgkjdd')
['com', 'org']

Required Output:-
['abcdef@aaaa.com']
['abcdef@aaaa.com', 'bsdkbsbd@bkdjb.org']
4

1 回答 1

1

the brackets at the end capture only the last group, that is com and org.

Change your regex to:

[a-zA-Z0-9][\w\.]{4}[\w\.]*@[a-zA-Z0-9]+[.](?:com|co\.in|org|edu)
                                            ^^

This ensures that no groups are matched, so that the matching string is stored instead of the grouped part only.

Also, I'm not sure why you put all those commas. I removed them and the regex is still working.

于 2013-05-29T18:01:01.010 回答