gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:34:02 26 4
gpt4 key购买 nike

我是 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)的列表。

最佳答案

你可以使用 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']

关于python - 在 Python 中删除字符串中特定子字符串前后的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40637118/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com