简体   繁体   English

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

[英]How to remove right side elements of a string with regex in python?

I am new to regex and python. 我是regex和python的新手。

My patterns are 我的模式是

'Contact: Order Procesing'
'Contact: ice.cream@cream.com'
'Contact: Opr & Packaging Supply'
'Contact: JOE (continued)'
'Contact: BOB/LORA/JACKIE'
'Contact: Ben - FTTC CER (continued)'

now I need to find the pattern to match contact and remove the entire string with a blank space. 现在,我需要找到匹配联系人的模式,并用空格删除整个字符串。

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

matches text with Contact. 将文本与联系人匹配。 The problem is I do not know how to remove the Contact and right part of the Contact. 问题是我不知道如何删除联系人和联系人的右侧部分。

Is there any most efficient pythonic way to do this 有没有最有效的pythonic方法来做到这一点

Use re.sub 使用re.sub

Ex: 例如:

import re

d = ['Contact: Order Procesing', 'Contact: ice.cream@cream.com', '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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM