[英]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. 我需要使用
python
从下面的NAME
中删除%22
并将其替换为空白。
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.
或者,如果您对正则表达式感到疯狂 ...就像您在循环遍历大量查找/替换参数一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.