Python + 正则表达式:AttributeError:'NoneType' 对象没有属性 'groups'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15232832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python + Regex: AttributeError: 'NoneType' object has no attribute 'groups'
提问by jO.
I have a string which I want to extract a subset of. This is part of a larger Python script.
我有一个字符串,我想从中提取一个子集。这是一个更大的 Python 脚本的一部分。
This is the string:
这是字符串:
import re
htmlString = '</dd><dt> Fine, thank you. </dt><dd> Molt bé, gràcies. (<i>mohl behh, GRAH-syuhs</i>)'
Which I want to pull-out "Molt bé, gràcies. mohl behh, GRAH-syuhs". And for that I use regular expression using re.search:
我想拉出“ Molt bé, gràcies. mohl behh, GRAH-syuhs”。为此,我使用正则表达式re.search:
SearchStr = '(\<\/dd\>\<dt\>)+ ([\w+\,\.\s]+)([\&\#\d\;]+)(\<\/dt\>\<dd\>)+ ([\w\,\s\w\s\w\?\!\.]+) (\(\<i\>)([\w\s\,\-]+)(\<\/i\>\))'
Result = re.search(SearchStr, htmlString)
print Result.groups()
AttributeError: 'NoneType' object has no attribute 'groups'
Since Result.groups()doesn't work, neither do the extractions I want to make (i.e. Result.group(5)and Result.group(7)).
But I don't understand why I get this error? The regular expression works in TextWrangler, why not in Python? Im a beginner in Python.
由于Result.groups()不起作用,我想做的提取(即Result.group(5)和Result.group(7))也不起作用。但我不明白为什么我会收到这个错误?正则表达式在 TextWrangler 中有效,为什么不在 Python 中?我是 Python 的初学者。
采纳答案by antonavy
import re
htmlString = '</dd><dt> Fine, thank you. </dt><dd> Molt bé, gràcies. (<i>mohl behh, GRAH-syuhs</i>)'
SearchStr = '(\<\/dd\>\<dt\>)+ ([\w+\,\.\s]+)([\&\#\d\;]+)(\<\/dt\>\<dd\>)+ ([\w\,\s\w\s\w\?\!\.]+) (\(\<i\>)([\w\s\,\-]+)(\<\/i\>\))'
Result = re.search(SearchStr.decode('utf-8'), htmlString.decode('utf-8'), re.I | re.U)
print Result.groups()
Works that way. The expression contains non-latin characters, so it usually fails. You've got to decode into Unicode and use re.U (Unicode) flag.
这样工作。该表达式包含非拉丁字符,因此通常会失败。您必须解码为 Unicode 并使用 re.U (Unicode) 标志。
I'm a beginner too and I faced that issue a couple of times myself.
我也是初学者,我自己也遇到过几次这个问题。
回答by thkang
You are getting AttributeErrorbecause you're calling groupson None, which hasn't any methods.
你得到AttributeError,因为你调用groups上None,还没有任何方法。
regex.searchreturning Nonemeans the regex couldn't find anything matching the pattern from supplied string.
regex.search返回None意味着正则表达式无法从提供的字符串中找到与模式匹配的任何内容。
when using regex, it is nice to check whether a match has been made:
使用正则表达式时,最好检查是否已进行匹配:
Result = re.search(SearchStr, htmlString)
if Result:
print Result.groups()
回答by Himadri Ranjan Das
import re
your_multicastip = "239.255.666.275"
if([224<=int(x)<240 for x in re.split('\.',re.match(r"^2(?:2[4-9]|3\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d?|0)){3}$",your_multicastip).groups())].count(True)==1):
print("%s is a valid multicast ip address",your_multicastip)
else:
print("%s is not a valid multicast ip address",your_multicastip)
Stacktrace:
堆栈跟踪:
Traceback (most recent call last):
File "source_file.py", line 3, in <module>
if([224<=int(x)<240 for x in re.split('\.',re.match(r"^2(?:2[4-9]|3\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d?|0)){3}$",your_multicastip).groups())].count(True)==1):
AttributeError: 'NoneType' object has no attribute 'groups'
I am python beginner and i faced this issue for 'else' case.
我是 python 初学者,我在“其他”情况下遇到了这个问题。

