2016-03-03 80 views
1

我使用https://regex101.com/#python来构建regx搜索的以下模式。虽然它在构建器网站中工作正常,但它似乎在我的python脚本中失败。回溯表明AttributeError: 'NoneType' object has no attribute 'group'Python正则表达式re.compile搜索失败

raw = [u'\n#\n# ARIN WHOIS data and services are subject to the Terms of Use\n# available at: https://www.arin.net/whois_tou.html\n#\n# If you see inaccuracies in the results, please report at\n# https://www.arin.net/public/whoisinaccuracy/index.xhtml\n#\n\n\n#\n# Query terms are ambiguous. The query is assumed to be:\n#  "n 132.245.55.8"\n#\n# Use "?" to get help.\n#\n\n#\n# The following results may also be obtained via:\n# https://whois.arin.net/rest/nets;q=132.245.55.8?showDetails=true&showARIN=false&showNonArinTopLevelNet=false&ext=netref2\n#\n\nNetRange:  132.245.0.0 - 132.245.255.255\nCIDR:   132.245.0.0/16\nNetName:  MICROSOFT\nNetHandle:  NET-132-245-0-0-1\nParent:   NET132 (NET-132-0-0-0-0)\nNetType:  Direct Assignment\nOriginAS:  \nOrganization: Microsoft Corp (MSFT-Z)\nRegDate:  2011-06-22\nUpdated:  2013-08-20\nRef:   https://whois.arin.net/rest/net/NET-132-245-0-0-1\n\n\n\nOrgName:  Microsoft Corp\nOrgId:   MSFT-Z\nAddress:  One Microsoft Way\nCity:   Redmond\nStateProv:  WA\nPostalCode:  98052\nCountry:  US\nRegDate:  2011-06-22\nUpdated:  2015-10-28\nComment:  To report suspected security issues specific to \nComment:  traffic emanating from Microsoft online services, \nComment:  including the distribution of malicious content \nComment:  or other illicit or illegal material through a \nComment:  Microsoft online service, please submit reports \nComment:  to:\nComment:  * https://cert.microsoft.com. \nComment:  \nComment:  For SPAM and other abuse issues, such as Microsoft \nComment:  Accounts, please contact:\nComment:  * [email protected] \nComment:  \nComment:  To report security vulnerabilities in Microsoft \nComment:  products and services, please contact:\nComment:  * [email protected] \nComment:  \nComment:  For legal and law enforcement-related requests, \nComment:  please contact:\nComment:  * [email protected]\nComment:  \nComment:  For routing, peering or DNS issues, please \nComment:  contact:\nComment:  * [email protected]\nRef:   https://whois.arin.net/rest/org/MSFT-Z\n\n\nOrgTechHandle: MRPD-ARIN\nOrgTechName: Microsoft Routing, Peering, and DNS\nOrgTechPhone: +1-425-882-8080 \nOrgTechEmail: [email protected]\nOrgTechRef: https://whois.arin.net/rest/poc/MRPD-ARIN\n\nOrgAbuseHandle: MAC74-ARIN\nOrgAbuseName: Microsoft Abuse Contact\nOrgAbusePhone: +1-425-882-8080 \nOrgAbuseEmail: [email protected]\nOrgAbuseRef: https://whois.arin.net/rest/poc/MAC74-ARIN\n\n\n#\n# ARIN WHOIS data and services are subject to the Terms of Use\n# available at: https://www.arin.net/whois_tou.html\n#\n# If you see inaccuracies in the results, please report at\n# https://www.arin.net/public/whoisinaccuracy/index.xhtml\n#\n\n'] 

pattern_strings = ['address:(.*?)(?=\\nphone:)', 'Address(.*?)(?=\\nRegDate:)'] 
pattern_string = '|'.join(pattern_strings) 
address_t = re.compile(pattern_string) 
address_t2 = address_t.search(raw)    
if address_t2.group(0): 
    address = address_t2.group(0) 
if address_t2.group(1): 
    address = address_t2.group(1) 
+0

在您的正则表达式替换\\与\。需要 –

+0

没了,这并不工作,并停止在文字上的''\ nphone:'或'\ nRegDate:' – iNoob

+1

'\ nphone:'是不是真的'\ nphone:',它是一个换行符之后'电话: '。尝试'print(raw [0])'看看你自己。 – Chris

回答

3

在第二种方式表达,Address(.*?)(?=\\nRegDate:),你似乎想.匹配任何字符,包括换行,这是不正常的意义。

试试这个:

address_t = re.compile(pattern_string, re.DOTALL) 
+0

罗布现货,感谢您花时间查看问题和可用数据。 – iNoob

+1

不客气,但它花了我比我们更长的时间,因为您的测试程序实际上并不是您的测试程序。我必须修复两个语法错误才能运行。将来,请**复制粘贴**(不要重新键入或总结)您的整个**(例如,不跳过“导入”)短程序,以显示错误。这是我实际测试的程序:http://ideone.com/dYYJHc。有关提问的更多提示,请参见[问]和[mcve]。 –