Python 3 re.sub的奇怪行为
以下代码:Python 3 re.sub的奇怪行为,python,python-3.x,regex,re,Python,Python 3.x,Regex,Re,以下代码: import re print(re.sub('[^a-zA-Z0-9]', '', ',Inc.', re.IGNORECASE).lower()) print(re.sub('[^a-zA-Z0-9]', '', ', Inc.', re.IGNORECASE).lower()) 产生: inc inc. 为什么?从中,签名是: re.sub(pattern, repl, string, count=0, flags=0) 因此,让我们基于此检查您的通话: re.sub(
import re
print(re.sub('[^a-zA-Z0-9]', '', ',Inc.', re.IGNORECASE).lower())
print(re.sub('[^a-zA-Z0-9]', '', ', Inc.', re.IGNORECASE).lower())
产生:
inc
inc.
为什么?从中,签名是:
re.sub(pattern, repl, string, count=0, flags=0)
因此,让我们基于此检查您的通话:
re.sub('[^a-zA-Z0-9]', '' , ', Inc.', re.IGNORECASE) # default
# < pattern > <repl> <string> < count > <flags>
re.sub
的第四个参数是count,而不是flags。应该是re.sub(“[^a-zA-Z0-9]”,“,”,Inc.,0,re.IGNORECASE)
知道我忘记了一些愚蠢的事情。谢谢
>>> re.sub('[^a-zA-Z0-9]', '', ', Inc.', flags=re.IGNORECASE).lower()
'inc'