想使用正則表達式匹配,提取文本中特定的內容。
文章目錄
問題描述:
問題遇到的現象和發生背景:
工作需要在文本中,查找指定的內容,輸出到其他文本。
問題相關代碼,請勿粘貼截圖
import os, re
with open (r'C:\\Users\\tjing\\Desktop\\python\\st.txt','r+') as f:
1 2 3 4 5 6 7 8 |
Keystart=<span>'ONOFFLINE : ON LINE '</span> Keyend=<span>'Warning:Site-01, ### remote_fetch(cim_TestResult) ### errr!!!'</span> buff = f.<span>read</span>() pat = re.<span>compile</span>(Keystart+<span>'(.*?)'</span>+Keyend, re.S) result = pat.<span>findall</span>(buff) <span>print</span> (result) |
附上相關文檔
[GPIB]W: (BINON:11111111,11111111,11111111,11111111,11111111,11111111,1111111A,11111A11,11111111,1111111A,1111111A,111A111A,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11A11111,111A1111,A1111111,111111A1,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,1111111A,11111111,11111111,11111A11,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111;
ONOFFLINE : ON LINE
Hello, python!
Warning:Site-01, ### remote_fetch(cim_TestResult) ### errr!!!
[GPIB]W: (BINON:11111111,11111111,11111111,11111111,11111111,11111111,1111111A,11111A11,11111111,1111111A,1111111A,111A111A,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11A11111,111A1111,A1111111,111111A1,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,1111111A,11111111,11111111,11111A11,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111;
運行結果及報錯內容
結果一直是空的。
我的解答思路和嘗試過的方法
嘗試用正則表達式匹配指定內容。
我想要達到的結果
能夠讀取相應始末位置間的內容。
網友觀點:
這應該就是你想要的功能:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<span>import</span> os, re <span>def</span> <span>GetMiddleStr</span>(<span>content,startStr,endStr</span>): <span>'''提取字元串content當中,startStr與endStr之間的內容 '''</span> startIndex = content.index(startStr) <span>if</span> startIndex>=<span>0</span>: startIndex += <span>len</span>(startStr) endIndex = content.index(endStr) <span>return</span> content[startIndex:endIndex] <span>with</span> <span>open</span> (<span>r'C:\\Users\\tjing\\Desktop\\python\\st.txt'</span>,<span>'r+'</span>) <span>as</span> f: Keystart=<span>r'ONOFFLINE : ON LINE'</span> Keyend=<span>r'Warning:Site-01, ### remote_fetch(cim_TestResult) ### errr!!!'</span> buff = f.read() result = GetMiddleStr(buff,Keystart,Keyend) result = result.strip() <span># 清除前後的換行符</span> <span>print</span>(result) |
或者可以將你的源代碼改為如下代碼:
1 2 3 4 5 6 7 8 9 10 11 |
<span>import</span> os, re <span>with</span> <span>open</span> (<span>r'C:\\Users\\tjing\\Desktop\\python\\st.txt'</span>,<span>'r+'</span>) <span>as</span> f: Keystart=<span>r'ONOFFLINE : ON LINE'</span> <span># 將()寫為\\(\\),因為要匹配這個符號本身</span> Keyend=<span>r'Warning:Site-01, ### remote_fetch\\(cim_TestResult\\) ### errr!!!'</span> buff = f.read() pat = re.<span>compile</span>(Keystart+<span>r'(.*?)'</span>+Keyend, re.S) result = pat.findall(buff) <span>print</span>(result) |
你的代碼沒能正確匹配的原因是,Keyend字元串內有英文括弧,這在正則表達式裡面是特殊符號。
我建議使用第一種方法,這樣,各種特殊符號都不需要特殊處理。
覺得有幫助請採納本答案,謝謝哦!
問題原因是在於你的Keyend中有(cim_TestResult) ,Keyend這個字元串里只要不包含(cim_TestResult),都能匹配成功並輸出對應內容。
貌似涉及到了轉義和原生字元問題。或者可以在 '(' ')' 前添加
Keyend='Warning:Site-01, ### remote_fetch(cim_TestResult) ### errr!!!'
如何利用正則表達式匹配以下內容?
說了那麼多,你的示例呢?
1. 帶 [親測] 說明源碼已經被站長親測過!
2. 下載後的源碼請在24小時內刪除,僅供學慣用途!
3. 分享目的僅供大家學習和交流,請不要用於商業用途!
4. 本站資源售價只是贊助,收取費用僅維持本站的日常運營所需!
5. 本站所有資源來源於站長上傳和網路,如有侵權請郵件聯繫站長!
6. 沒帶 [親測] 代表站長時間緊促,站長會保持每天更新 [親測] 源碼 !
7. 盜版ripro用戶購買ripro美化無擔保,若設置不成功/不生效我們不支持退款!
8. 本站提供的源碼、模板、插件等等其他資源,都不包含技術服務請大家諒解!
9. 如果你也有好源碼或者教程,可以到審核區發布,分享有金幣獎勵和額外收入!
10.如果您購買了某個產品,而我們還沒來得及更新,請聯繫站長或留言催更,謝謝理解 !
GG資源網 » 想使用正則表達式匹配,提取文本中特定的內容。