简体   繁体   中英

Python regex remove and replace characters

I need to use python to remove the %22 from the NAME below and replace it with a blank space.

How would that be done?

NAME = 'best%22buy'

使用字符串替换不是正则表达式。

NAME = NAME.replace("%22", ' ')

NAME='best%22buy'.replace('%22',' ')

To answer your question, if you really want to use regex for this, I'd do:

 re.sub(r'\%22', '', 'best%22buy')

Otherwise, I'd do what everyone else is saying and do a find-replace:

'best%22buy'.replace('%22', '')

The second option is better in my opinion, unless you have a ton of other regex in the same area and you want to keep everything consistent. Or if you are going crazy with regex... like you re looping through a ton of find/replace parameters.

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