我有以下格式的字符串name1 <[email protected]>
。如何使用正则表达式仅拉出name1部分?另外,如果我有多个这样的名称和电子邮件,name1 <[email protected]>, name2 <[email protected]>
怎么办呢?
您可以从开始(\w+)\s<.*?>(?:,\s)?
(请参阅regex101.com)开始,它依赖于电子邮件被包围的事实< >
,并在您认为合适的情况下对其进行自定义。
请注意,此正则表达式不专门查找电子邮件,仅查找由包围的文本< >
。
import re
regex = re.compile(r'(\w+)\s<.*?>(?:,\s)?')
string = 'name1 <[email protected]>, name2 <[email protected]>'
打印([匹配regex.findall(string)中的匹配项])
输出
['name1', 'name2']
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句