【发布时间】:2016-11-16 16:20:07
【问题描述】:
我是 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"
我有要在字符串中搜索的这些首字母缩写词(MP,TP)的列表。
【问题讨论】:
-
看看regex模块的替代函数re.sub
-
TP 前后的任何内容。它可以包含数字和字符。这个东西1234578TP43789应该在输出中替换为TP。
标签: python regex regex-lookarounds