import re
s=open(r"bug.txt").read()
regexpr=re.compile(r"start(.*?)end",re.DOTALL)
result=regexpr.search(s)
try:
print result.group(1)
except:
print "Can't find match string"
本文介绍了一种使用Python的正则表达式模块(re)从文件中提取特定字符串的方法。通过定义正则表达式模式并应用search方法,可以从目标文件中匹配并输出所需的内容。如果未找到匹配项,则输出提示信息。
import re
s=open(r"bug.txt").read()
regexpr=re.compile(r"start(.*?)end",re.DOTALL)
result=regexpr.search(s)
try:
print result.group(1)
except:
print "Can't find match string"

被折叠的 条评论
为什么被折叠?