删除 Python 中字符串中特定字符串前后的字符
Remove characters before and after particular subtring in a string in Python
我是 Python.May 的新手,这可以通过 regex.I 来完成 想在字符串中搜索特定的子字符串并删除字符串中前后的字符。
示例 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"
我有要在字符串中搜索的这些缩写词(MP
、TP
)的列表。
您可以使用re.sub
>>> string="This is the consignment no 1234578TP43789"
>>> re.sub(r'\d+(TP|MP)\d+', r'', 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'', string)
'Consignment no TP is on its way on vehicle no MP'
它有什么作用?
\d+
匹配一位或多位数字。
(TP|MP)
匹配 TP
或 MP
。在 </code> 中捕获它。我们使用这个捕获的字符串来替换整个匹配的字符串。</li>
</ul>
<hr>
<p>如果任何字符可以出现在 TP/MP 之前和之后,我们可以使用 <code>\S
来匹配 space 以外的任何字符。例如,
>>> string="Consignment no 1234578TP43789 is on its way on vehicle no 3456MP567890"
>>> re.sub(r'\S+(TP|MP)\S+', r'', 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'', string) for string in list_2 ]
['This is the consignment no TP', 'Consignment no TP is on its way on vehicle no MP']
您可以使用 strip
去除字符串前后的字符。
strg="Consignment no 1234578TP43789 is on its way on vehicle no 3456MP567890"
strg=' '.join([word.strip('0123456789') 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('0123456789') 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
我是 Python.May 的新手,这可以通过 regex.I 来完成 想在字符串中搜索特定的子字符串并删除字符串中前后的字符。
示例 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"
我有要在字符串中搜索的这些缩写词(MP
、TP
)的列表。
您可以使用re.sub
>>> string="This is the consignment no 1234578TP43789"
>>> re.sub(r'\d+(TP|MP)\d+', r'', 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'', string)
'Consignment no TP is on its way on vehicle no MP'
它有什么作用?
\d+
匹配一位或多位数字。(TP|MP)
匹配TP
或MP
。在</code> 中捕获它。我们使用这个捕获的字符串来替换整个匹配的字符串。</li> </ul> <hr> <p>如果任何字符可以出现在 TP/MP 之前和之后,我们可以使用 <code>\S
来匹配 space 以外的任何字符。例如,>>> string="Consignment no 1234578TP43789 is on its way on vehicle no 3456MP567890" >>> re.sub(r'\S+(TP|MP)\S+', r'', 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'', string) for string in list_2 ] ['This is the consignment no TP', 'Consignment no TP is on its way on vehicle no MP']
您可以使用 strip
去除字符串前后的字符。
strg="Consignment no 1234578TP43789 is on its way on vehicle no 3456MP567890"
strg=' '.join([word.strip('0123456789') 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('0123456789') 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