2016-03-03 74 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]。 –