想使用正则表达式匹配,提取文本中特定的内容。
文章目录
问题描述:
问题遇到的现象和发生背景:
工作需要在文本中,查找指定的内容,输出到其他文本。
问题相关代码,请勿粘贴截图
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资源网 » 想使用正则表达式匹配,提取文本中特定的内容。