對于我的碩士論文,我需要從公司收入電話記錄中提取(演講者、文本)元組。成績單采用以下形式:OPERATOR: Some text with numbers, special characters and linebreaks.NAME, COMPANY, POSITION: Some text with numbers, special characters and linebreaks.NAME: Some text with numbers, special characters and linebreaks.我想從文檔中提取所有(揚聲器、文本)元組。例如:[("OPERATOR", "Some text with numbers, special characters and linebreaks."), ..]到目前為止,我已經用re.findallPython 中的函數(shù)嘗試了不同的正則表達式。這是我的代碼:import re# First approach:r = re.compile(r"^([^a-z:]+?):([\s\S]+?)", flags=re.MULTILINE)re.findall(r, example)# Second approach:r = re.compile(r"^([^a-z:]+?):([\s\S]+)", flags=re.MULTILINE)re.findall(r, example)第一種(非貪婪)方法的問題在于它沒有捕獲說話者的全文。第二種(貪婪)方法的問題在于,它不會在下一個說話者出現(xiàn)時停止。編輯:附加信息文本組也可以包含雙點。在某些情況下,在一行的第一個單詞之后立即出現(xiàn)雙點,例如“For\nexample: ...”演講者組也可以覆蓋多行,例如當公司名稱和職位描述很長時
如何使用正則表達式從獲得通話記錄中提?。〒P聲器、文本)元組?
慕無忌1623718
2021-10-10 15:22:58