簡體   English   中英

Python正則表達式re.compile搜索失敗

[英]Python regex re.compile search failing

我使用https://regex101.com/#python為regx搜索構建以下模式。 雖然它在構建器站點中工作正常,但它似乎在我的python腳本中失敗了。 回溯建議使用AttributeError: 'NoneType' object has no attribute 'group'

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:        * abuse@microsoft.com.  \nComment:        \nComment:        To report security vulnerabilities in Microsoft \nComment:        products and services, please contact:\nComment:        * secure@microsoft.com.  \nComment:        \nComment:        For legal and law enforcement-related requests, \nComment:        please contact:\nComment:        * msndcc@microsoft.com\nComment:        \nComment:        For routing, peering or DNS issues, please \nComment:        contact:\nComment:        * IOC@microsoft.com\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:  IOC@microsoft.com\nOrgTechRef:    https://whois.arin.net/rest/poc/MRPD-ARIN\n\nOrgAbuseHandle: MAC74-ARIN\nOrgAbuseName:   Microsoft Abuse Contact\nOrgAbusePhone:  +1-425-882-8080 \nOrgAbuseEmail:  abuse@microsoft.com\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)

在第二個替代表達式中, Address(.*?)(?=\\\\nRegDate:) ,您似乎想要. 匹配任何字符,包括換行符,這不是它的正常含義。

嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM