简体   繁体   中英

Python Regex - How to remove text between 2 characters

How can I remove anything between ")" and "|"

For example,

str = "left)garbage|right"

I need the output to be "left)|right"

>>> import re
>>> s = "left)garbage|right"
>>> re.sub(r'(?<=\)).*?(?=\|)', '', s)
'left)|right'

>>> re.sub(r'\).*?\|', r')|', s)
'left)|right'

在你的具体情况下,它是

str[:str.index(')')+1] + str[str.index('|'):]
>>> import re
>>> str = 'left)garbage|right'
>>> re.sub(r"\).*?\|",")|",str)
'left)|right'

You can do this:

import re
str = "left)garbage|right"
re.sub(r"(?<=\)).*?(?=\|)", "", str)

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