python正则表达式,提取出由beginning开始到第一个end结束的一段文字_百...
发布网友
发布时间:2022-04-27 02:42
我来回答
共3个回答
热心网友
时间:2023-01-25 04:17
python中正则是无法做到跨行匹配的,你自己写函数解析,比如 :
xyz = """
bug.txt
--beginning
50-23 2323 dsfal ksldfklad ds ljk sdklfjl
aklsdjlf kl 90 jklsdj- klajsdl kldafjkl
skdlfjksdl
......
[end]
sdkljfkls ksldfjlsd l jklsdjflasdf ljsdflsd
sdfsdf 23749 wejklj 9808 ljldf
sdlfj u70 kjsdklf
...
[end]
"""
lines = xyz.splitlines()
start = -1
end = 0
for i, line in enumerate(lines):
if start == -1 and line == "--beginning":
start = i
elif not start == -1 and line == "[end]":
end = i
break
if not end == 0:
print "\n".join(lines[start+1:end])
热心网友
时间:2023-01-25 05:35
import re
s=open(r"bug.txt").read()
regexpr=re.compile(r"beginning(.*?)\[end\]",re.DOTALL)
result=regexpr.search(s)
try:
print result.group(1)
except:
print "Can't find match string"
热心网友
时间:2023-01-25 07:10
--beginning(.*?)\[end\]