Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有以下正则表达式,它从字符串文本中删除所有没有字母数字字符
re.sub(r'[^a-zA-Z0-9]',' ', text)
如何修改此表达式以在字符串 text 中包含字符 '[' 和 ']' ?
[将,添加到带有转义]的字符类 ( ) 中。[ .. ]
[
]
[ .. ]
re.sub(r'[^a-zA-Z0-9\[\]]',' ', text)
例子:
>>> re.sub(r'[^a-zA-Z0-9\[\]]', ' ', 'a,b[c-d]!') 'a b[c d] '