亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何忽略正則表達式中不需要的模式

如何忽略正則表達式中不需要的模式

守著一只汪 2023-05-09 10:33:55
我有以下 python 代碼from io import BytesIOimport pdfplumber, requeststest_case = {    'https://www1.hkexnews.hk/listedco/listconews/sehk/2020/0514/2020051400555.pdf': 59,    'https://www1.hkexnews.hk/listedco/listconews/gem/2020/0529/2020052902118.pdf': 55,    'https://www1.hkexnews.hk/listedco/listconews/sehk/2020/0618/2020061800366.pdf': 47,    'https://www1.hkexnews.hk/listedco/listconews/gem/2020/0630/2020063002674.pdf': 30,}for url, page in test_case.items():    rq = requests.get(url)    pdf = pdfplumber.load(BytesIO(rq.content))    txt = pdf.pages[page].extract_text()    txt = re.sub("([^\x00-\x7F])+", "", txt)  # no chinese    pattern = r'.*\n.*?(?P<auditor>[A-Z].+?\n?)(?:LLP\s*)?\s*((PRC.*?|Chinese.*?)?[Cc]ertified [Pp]ublic|[Cc]hartered) [Aa]ccountants'    try:        auditor = re.search(pattern, txt, flags=re.MULTILINE).group('auditor').strip()        print(repr(auditor))    except AttributeError:        print(txt)        print('============')        print(url)它產生以下結果'ShineWing''ShineWing''Hong Kong Standards on Auditing (HKSAs) issued by the Hong Kong Institute of''Hong Kong Financial Reporting Standards issued by the Hong Kong Institute of'期望的結果是:'ShineWing''ShineWing''Ernst & Young''Elite Partners CPA Limited'我試過:pattern = r'.*\n.*?(?P<auditor>[A-Z].+?\n?)$(?!Institute)(?:LLP\s*)?\s*((PRC.*?|Chinese.*?)?[Cc]ertified [Pp]ublic|[Cc]hartered) [Aa]ccountants' 此模式捕獲后兩種情況,但不捕獲前 2 種情況。pattern = r'.*\n.*?(?P<auditor>^(?!Hong|Kong)[A-Z].+?\n?)(?:LLP\s*)?\s*((PRC.*?|Chinese.*?)?[Cc]ertified [Pp]ublic|[Cc]hartered) [Aa]ccountants' 這會產生所需的結果,但^(?!Hong|Kong)存在潛在風險,因為它可能會在未來忽略其他所需的結果,因此它不是一個好的候選者。相反,$(?!Institute)更通用和合適,但我不知道為什么它在前兩種情況下無法匹配。如果有一種方法可以忽略包含的匹配項,那就太好了issued by the Hong Kong Institute of任何建議將不勝感激。謝謝。
查看完整描述

1 回答

?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

pattern = r'\n.*?(?P<auditor>(?!.*Institute)[A-Z].+?)(?:LLP\s*)?\s*((PRC.*?|Chinese.*?)?[Cc]ertified [Pp]ublic|[Cc]hartered) [Aa]ccountants'

這行得通。


查看完整回答
反對 回復 2023-05-09
  • 1 回答
  • 0 關注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號