簡體   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