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.
我正在尝试从字符串中过滤掉不一致的字符。
目前我有这个..在 python
name = re.sub('([^a-zA-Z0-9 -\'!$&])',' ', name)
但我试图包含诸如“()”括号“/”反斜杠和正斜杠之类的字符。不知何故,它不起作用。有没有人可以看看。。
非正则表达式解决方案
accepted = '''!$*()\/.,>-_=+<:;'"?|''' allowed = string.digits + string.letters + accepted filter(allowed.__contains__, name)
这将过滤非字母数字字符的字符串名称,并否定过滤接受的字符。
name = re.sub('([^a-zA-Z0-9\[\]\(,\)\+\/ \\-\'!$&])',' ', name)