标签: python regex
我目前有一行address = re.sub('[^A-Za-z0-9]+', ' ', address).lstrip(),它会从我的字符串address中删除所有特殊字符。如何修改此行以保留#?
address = re.sub('[^A-Za-z0-9]+', ' ', address).lstrip()
address
#
答案 0 :(得分:6)
为了避免删除哈希符号,您需要将其添加到negated character class:
r'[^A-Za-z0-9#]+' ^
请参阅regex demo