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.
pattern = r'[ -\\[\\]]' regex = re.compile(pattern) name = '123[ shiv' new_name = regex.sub('_',name)
给出结果(新名称)::
'_____shiv'
代替::
'123__shiv'
..thanx提前
由于介于两者之间,您的正则表达式正在创建从whitespace (ASCII Code - 32)到opening bracket - [(ASCII Code - 91)-的范围。该范围包括数字0 to 9 (ASCII 码 - 48 到 57)。
whitespace
opening bracket - [
-
0 to 9
您需要将您的正则表达式更改为: -
pattern = '[- \\[\\]]'
一开始就动-了。