我试图使用re.sub从python中的字符串中删除符号:
re.sub(r"(?![a-z0-9])", "_", "some:long:str-:that:can't+have+symbols".lower())
我要的答案是:
some_long_str__that_can_t_have_symbols
但没用。我完全可以使用findall()然后join()来匹配字母数字字符,从而生成一个新字符串,但这将完全消除字符,因此我最终编写了一些效率低下的for循环。
我想问题在于我如何否定我的表达。有什么想法吗?
使用方式:
import re result = re.sub(r"([^a-z0-9])", "_", "some:long:str-:that:can't+have+symbols".lower()) print(result)
输出: