Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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行为_Python_Regex - Fatal编程技术网

正则表达式模块的奇怪Python行为

正则表达式模块的奇怪Python行为,python,regex,Python,Regex,我一直在使用Matthew Barnett的for Python,发现了一个奇怪的错误(行为?bug?)。 考虑下面的代码: import regex as re string = "liberty 123, equality 123, fraternity 123" rx = r'\d+(?=,|$)' results = re.findall(rx, string) print (results) 从Mac上的命令行调用(python regex.py)时,我会得到错误AttributeE

我一直在使用Matthew Barnett的for Python,发现了一个奇怪的错误(行为?bug?)。
考虑下面的代码:

import regex as re
string = "liberty 123, equality 123, fraternity 123"
rx = r'\d+(?=,|$)'
results = re.findall(rx, string)
print (results)
从Mac上的命令行调用(
python regex.py
)时,我会得到错误
AttributeError:“module”对象没有属性“findall”
,而当我将完全相同的代码复制并粘贴到python shell时,它会正确输出

['123', '123', '123']

谁能给我点灯吗?这是我在这里缺少的一些明显的东西吗?

您不能将模块命名为与系统模块相同的名称。将您的文件
regex.py
重命名为其他文件,如
my_regex.py
,然后删除该文件
regex.pyc
,如果它存在。

是否
regex.py
是您的文件?您是否将其命名为与您尝试导入的python包完全相同的名称?我猜它实际上是在导入本身,它不包含
findall()
@BrendanAbel:是的,这是个问题吗?您的文件名混淆了解释器,因为该名称与您导入的模块
regex
冲突。更改文件名,你应该表现得很好。@BrendanAbel:你是第一个,这就解决了问题。把它作为答案,我会接受的。该死的,就是这样:-)谢谢。