我使用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)
在您的正則表達式替換\\與\。需要 –
沒了,這並不工作,並停止在文字上的''\ nphone:'或'\ nRegDate:' – iNoob
'\ nphone:'是不是真的'\ nphone:',它是一個換行符之後'電話: '。嘗試'print(raw [0])'看看你自己。 – Chris