简体   繁体   中英

Python using re.sub to remove after string.

How do i remove everything after .rtf What I have so far removes .rtf and everything after it as well.

s = 'newyork.pdf.rtf.doc'
actual_file = re.sub(".rtf.+", "", s)

returns newyork.pdf

Option 1
Use a positive lookbehind (?<=\\.rtf).* :

>>> re.sub(r'(?<=\.rtf).*', '', s)
'newyork.pdf.rtf'

Option 2
Capture the .rtf and substitute the matching group back in:

>>> re.sub(r'(\.rtf).*', r'\1', s)
'newyork.pdf.rtf'

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