簡體   English   中英

Python正則表達式字符串組捕獲

[英]Python regex string groups capture

我有許多醫療報告,我試圖從每個報告中獲取 6 個組(第 5 組和第 6 組是可選的):

(臨床細節|臨床適應症)+(文本1)+(結果|報告)+(文本2)+(解釋|結論)+(文本3)。

我使用的正則表達式是:

reportPat=re.compile(r'(Clinical details|indication)(.*?)(result|description|report)(.*?)(Interpretation|conclusion)(.*)',re.IGNORECASE|re.DOTALL)

除了在缺少可選組的字符串上起作用外,它失敗了。我試過在 group5 后面放一個問號,如下所示:(Interpretation|conclusion)?(.*) 但隨后這個組被合並到 group4 中。 我正在粘貼兩個相互沖突的字符串(一個包含組 5/6,另一個沒有它)供人們測試他們的正則表達式。 謝謝你的幫助

文本 1(所有組都在場)

Technical Report:\\nAdministrations:\\n1.04 ml of Fluorine 18, fluorodeoxyglucose with aco - Bronchus and lung\\nJA - Staging\\n\\nClinical Details:\\nSquamous cell lung cancer, histology confirmed ?stage\\nResult:\\nAn FDG scan was acquired from skull base to upper thighs together with a low dose CT scan for attenuation correction and image fusion. \\n\\nThere is a large mass noted in the left upper lobe proximally, with lower grade uptake within a collapsed left upper lobe. This lesi\\n\\nInterpretation: \\nThe scan findings are in keeping with the known lung primary in the left upper lobe and involvement of the lymph nodes as dThere is no evidence of distant metastatic disease.

文本 2(不含第 5 組和第 6 組)

Technical Report:\\nAdministrations:\\n0.81 ml of Fluorine 18, fluorodeoxyglucose with activity 312.79\\nScanner: 3D Static\\nPatient Position: Supine, Head First. Arms up\\n\\n\\nDiagnosis Codes:\\n- Bronchus and lung\\nJA - Staging\\n\\nClinical Indication:\\nNewly diagnosed primary lung cancer with cranial metastasis. PET scan to assess any further metastatic disease.\\n\\nScanner DST 3D\\n\\nSession 1 - \\n\\n.\\n\\nResult:\\nAn FDG scan was acquired from skull base to upper thighs together with a low dose CT scan for attenuation correction and image fusion.\\n\\nThere is increased FDG uptake in the right lower lobe mass abutting the medial and posterior pleura with central necrosis (maximum SUV 18.2). small nodule at the right paracolic gutte

似乎您缺少的基本上是模式匹配的結束,以在與組 5 和 6 的可選存在結合時愚弄貪婪匹配。這個正則表達式應該可以解決問題,保持您當前的組編號:

reportPat=re.compile(
   r'(Clinical details|indication)(.*)'
   r'(result|description|report)(.*?)'
   r'(?:(Interpretation|conclusion)(.*))?$',
   re.IGNORECASE|re.DOTALL)

所做的更改是將$添加到末尾,並將最后兩個組包含在可選的非捕獲組(?: ... )? . 還要注意如何通過拆分行(解釋器將在編譯時自動連接)輕松地使整個正則表達式更具可讀性。

添加:在查看匹配結果時,我看到了一些:\\n: \\n ,可以通過添加(?:[:\\s]*)? 在標題和文本組之間。 這是一個可選的非捕獲冒號和空格組。 你的正則表達式看起來像這樣:

reportPat=re.compile(
   r'(Clinical details|indication)(?:[:\s]*)?(.*)'
   r'(result|description|report)(?:[:\s]*)?(.*?)'
   r'(?:(Interpretation|conclusion)(?:[:\s]*)?(.*))?$',
   re.IGNORECASE|re.DOTALL)

補充 2:在這個鏈接: https : //regex101.com/r/gU9eV7/3 ,你可以看到正則表達式的作用。 我還添加了一些單元測試用例來驗證它是否適用於兩個文本,並且在 text1 中它與 text1 匹配,而對於 text2 它什么都沒有。 我使用這個parallel在python腳本中直接編輯來驗證我的答案。

以下模式適用於您的兩個測試用例,盡管考慮到您必須解析的數據格式,我不相信該模式適用於所有情況(例如,我添加了:在每個關鍵字匹配之后嘗試防止無意中與更常見的詞(如resultdescription )進行匹配):

re.compile(
    r'(Clinical details|indication):(.+?)(result|description|report):(.+?)((Interpretation|conclusion):(.+?)){0,1}\Z',
    re.IGNORECASE|re.DOTALL
    )

我將最后 2 個組分組並使用{0,1}將它們標記為可選。 這意味着輸出組將與您的原始模式略有不同(您將有一個額外的組,第 4 組現在將包含最后 2 組的輸出,最后 2 組的數據將在第 5 組和6).

暫無
暫無

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

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