Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3 re.sub的奇怪行为_Python_Python 3.x_Regex_Re - Fatal编程技术网

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'