簡體   English   中英

如何使用 Python re.sub 刪除字符串后的所有內容

[英]how to remove everything after a string using Python re.sub

如果我有這樣的句子:

To contact me, please dial in the number: 300801

我想在“撥號”后刪除所有內容以保留:

To contact me, please dial

我怎樣才能使用 re.sub 來做到這一點?

完整的口譯課程:

>>> s = "To contact me, please dial in the number: 300801"
>>> import re
>>> re.sub("dial .*", "dial", s)
'To contact me, please dial'

不過,使用標准字符串切片和string.find() (如對您的問題的評論所示)可能更容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM