如何在python中使用正则表达式删除字符串的右侧元素?

nfg76nw0  于 8个月前  发布在  Python
关注(0)|答案(2)|浏览(78)

我是正则表达式和Python的新手。
我的模式是

'Contact: Order Procesing'
'Contact: [email protected]'
'Contact: Opr & Packaging Supply'
'Contact: JOE (continued)'
'Contact: BOB/LORA/JACKIE'
'Contact: Ben - FTTC CER (continued)'

现在我需要找到匹配contact的模式,并删除带有空格的整个字符串。

re.findall(r"Contact:",text)

将文本与联系人匹配。问题是我不知道如何删除联系人和正确的联系人的一部分。
有没有什么最有效的pythonic方法可以做到这一点

ogsagwnx

ogsagwnx1#

使用re.sub

例如:

import re

d = ['Contact: Order Procesing', 'Contact: [email protected]', 'Contact: Opr & Packaging Supply', 'Contact: JOE (continued)', 'Contact: BOB/LORA/JACKIE', 'Contact: Ben - FTTC CER (continued)']

for i in d:
    print(re.sub(r"^Contact:.*", "", i))
b5buobof

b5buobof2#

如果列表中的所有字符串都以子字符串“Contact:',使用此代码:

new_list = [string[10:] for string in old_list]

相关问题