Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.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正则表达式问题_Python_Regex_Python 3.x - Fatal编程技术网

Python 3正则表达式问题

Python 3正则表达式问题,python,regex,python-3.x,Python,Regex,Python 3.x,为什么以下string1regexp不匹配?我已经使用进行了测试,我的正则表达式fu似乎是准确的,所以我想我在python实现中肯定遗漏了一些东西: import re pattern = r".*W([0-9]+(\.5)?)[^\.]?.*$" string1 = '6013-SFR6W4.5' string2 = '6013-SFR6W4.5L' print(re.match(pattern, string1)) # the return value is None print(re.ma

为什么以下
string1
regexp不匹配?我已经使用进行了测试,我的正则表达式fu似乎是准确的,所以我想我在python实现中肯定遗漏了一些东西:

import re
pattern = r".*W([0-9]+(\.5)?)[^\.]?.*$"
string1 = '6013-SFR6W4.5'
string2 = '6013-SFR6W4.5L'
print(re.match(pattern, string1)) # the return value is None
print(re.match(pattern, string2)) # this returns a re.match object
是显示此问题的交互式会话的屏幕截图

编辑


sys.version outputs 3.4.3

当我运行您提供的代码时,我会得到以下两个方面的返回值:

$ python3 test.py
<_sre.SRE_Match object at 0x6ffffedc3e8>
<_sre.SRE_Match object at 0x6ffffedc3e8>
$python3 test.py

我尝试了完全相同的代码,两种情况下我都有匹配项:

蟒蛇3.4:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> pattern = r".*W([0-9]+(\.5)?)[^\.]?.*$"
>>> string1 = '6013-SFR6W4.5'
>>> print(re.match(pattern, string1))
<_sre.SRE_Match object; span=(0, 13), match='6013-SFR6W4.5'>
>>> string2 = '6013-SFR6W4.5L'
>>> print(re.match(pattern, string2))
<_sre.SRE_Match object; span=(0, 14), match='6013-SFR6W4.5L'>
Python 3.4.3(v3.4.3:9b73f1c3e601,2015年2月23日,02:52:03)
[GCC 4.2.1(苹果公司建造5666)(dot 3)]关于达尔文
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>进口稀土
>>>模式=r“*W([0-9]+(\.5)?[^\.]?*$”
>>>string1='6013-SFR6W4.5'
>>>打印(重新匹配(模式,字符串1))
>>>string2='6013-SFR6W4.5L'
>>>打印(重新匹配(图案,细线2))
python 2.7:

Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> pattern = r".*W([0-9]+(\.5)?)[^\.]?.*$"
>>> string1 = '6013-SFR6W4.5'
>>> print(re.match(pattern, string1))
<_sre.SRE_Match object at 0x10abf83e8>
>>> string2 = '6013-SFR6W4.5L'
>>> print(re.match(pattern, string2))
<_sre.SRE_Match object at 0x10abf83e8>
Python 2.7.6(默认,2014年9月9日,15:04:36)
[GCC 4.2.1达尔文兼容苹果LLVM 6.0(clang-600.0.39)]
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>进口稀土
>>>模式=r“*W([0-9]+(\.5)?[^\.]?*$”
>>>string1='6013-SFR6W4.5'
>>>打印(重新匹配(模式,字符串1))
>>>string2='6013-SFR6W4.5L'
>>>打印(重新匹配(图案,细线2))

尝试使用
pattern=r“^.*W([0-9]+(\.5)?)[^\.]?.*$”
,在开头使用
^

在您发布的代码中,您有:

pattern = r".*W([0-9]+(\.5)?)[^\.]?.*$"
但是在你截图的代码中,你有

pattern = r".*W([0-9]+(\.5)?)[^\.]+.*$"

(注意,第一个模式末尾附近的
在第二个模式中被替换为
+

但后面是“*”表示“.”必须匹配“零次或多次”。根据@wesanyer:对不起,你是对的。但现在我已经测试过了,您的代码似乎运行良好。我也在两个字符串上都得到了结果。
python--version
的输出是什么?好的,我上传的屏幕截图显示我的模式有一个输入错误,我在[^\.]后面有一个“+”,我应该删除这个问题吗?如果你使用:
pattern=r“^.*W([0-9]+(\.5)?[^\.]?*$”
?你还没有收到
?太好了!要更新答案,您缺少
模式
定义开头的
^