2016-11-16 202 views
1

我是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代替。 –

回答

7

您可以使用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

請再說明一下嗎? –

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 
相關問題