繁体   English   中英

Python正则表达式用模式替换字符串

[英]Python regex replace string with pattern

正则表达式为r'[Az\\d,\\-.\\ \\/\\n]{1,}' ,此正则表达式将允许字母数字+一些特殊字符。

我想替换不允许的字符。

我努力了,

re.sub(r'[A-z\d,\-.\ \/\n]{1,}', ' ', 'ASGHB 3 JHDSD eyg && ^&*hdbcd v%^&*B#$%^')

输出为

' && &* % &* #$% '

我想要原始字符串作为输出,并用空格替换特殊字符(不允许使用)。

预期输出: ASGHB 3 JHDSD eyg ^ hdbcd v ^ B ^如何实现?

您可以在这里找到有关re.sub的所有信息

关于您的问题。 您应该在设置之前使用^:

If the first character of the set is '^', all the characters that are not in the set will be matched. 
For example, [^5] will match any character except '5', and [^^] will match any character except '^'. 
^ has no special meaning if it’s not the first character in the set.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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