删除Python中字符串中特定子字符串前后的字符

问题描述:

我是Python新手。可以用regex来完成。我想在字符串中搜索特定的子字符串,并在字符串之前和之后删除字符串。删除Python中字符串中特定子字符串前后的字符

实施例1对

Input:"This is the consignment no 1234578TP43789" 
Output:"This is the consignment no TP" 

实施例2

Input:"Consignment no 1234578TP43789 is on its way on vehicle no 3456MP567890" 
Output:"Consignment no TP is on its way on vehicle no MP" 

我有这些缩写(MPTP)字符串中要被搜索的列表。

+1

看看正则表达式模块的替换功能,[应用re.sub](HTTPS内://文档.python.org/3.5/library/re.html#re.sub) – Olian04

+0

TP之前和之后。它可以同时包含数字和字符。这个东西1234578TP43789应该被输出中的TP代替。 –

您可以使用re.sub

>>> string="This is the consignment no 1234578TP43789" 
>>> re.sub(r'\d+(TP|MP)\d+', r'\1', string) 
'This is the consignment no TP' 

>>> string="Consignment no 1234578TP43789 is on its way on vehicle no 3456MP567890" 
>>> re.sub(r'\d+(TP|MP)\d+', r'\1', string) 
'Consignment no TP is on its way on vehicle no MP' 

它能做什么?

  • \d+匹配一个或多个数字。
  • (TP|MP)匹配TPMP。在\1中捕获它。我们使用这个捕获的字符串来替换整个匹配的字符串。

如果可以出现任何字符之前和TP/MP之后,我们就可以使用\S匹配一个空格其他任何东西。例如,

>>> string="Consignment no 1234578TP43789 is on its way on vehicle no 3456MP567890" 
>>> re.sub(r'\S+(TP|MP)\S+', r'\1', string) 
'Consignment no TP is on its way on vehicle no MP' 

编辑

使用list comprehension,你可以遍历列表和替换所有的字符串作为,

>>> list_1=["TP","MP","DCT"] 
>>> list_2=["This is the consignment no 1234578TP43789","Consignment no 1234578TP43789 is on its way on vehicle no 3456MP567890"] 
>>> [ re.sub(r'\d+(' + '|'.join(list_1) + ')\d+', r'\1', string) for string in list_2 ] 
['This is the consignment no TP', 'Consignment no TP is on its way on vehicle no MP'] 
+0

@ nu11p01n73RThanks很多 一件事 LIST_1 = “TP”, “MP”, “DCT”] list_2 = [ “这是货物没有1234578TP43789”,“寄售没有1234578TP43789是其对车辆的方式没有3456MP567890“] 现在我必须从list_1采取TP,MP在list_2的字符串中搜索并替换它们。如何做? –

+0

@SalmanBaqri您可以使用'join'作为''''.join([“TP”,“MP”,“DCT”])生成正则表达式,并使用它迭代“list_2”以生成所需的输出。你也可以使用[list comprehensions](https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions)。 – nu11p01n73R

+0

请再说明一下吗? –

您可以使用strip从前后条字符一个字符串。

strg="Consignment no 1234578TP43789 is on its way on vehicle no 3456MP567890" 
strg=' '.join([word.strip('') for word in strg.split()]) 
print(strg) # Consignment no TP is on its way on vehicle no MP 
如果一个保留字被包含

要刚刚剥离把它的环

strg="Consignment no 1234578TP43789 is on its way on vehicle no 3456MP567890 200DG" 
reserved=['MP','TP'] 
for res in reserved: 
    strg=' '.join([word.strip('') if (res in word) else word for word in strg.split()]) 
print(strg) # Consignment no TP is on its way on vehicle no MP 200DG