简体   繁体   中英

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

If i have sentence like:

To contact me, please dial in the number: 300801

and I want to remove everything after "dial" to remain:

To contact me, please dial

How can I use re.sub to do that?

Full interpreter session:

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

Using standard string slicing and string.find() (as shown in the comments to your question) might be easier though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM