我在文本文件中有很多START / STOP文本块。
start
value1 as5q
value3 dd9w
type b
value6 dp1p
stop
start
value1 as
value2 er
type a
stop
start
value1 as5
value2 er2
type c
value4 dd1
stop
当类型为“a”时,我需要提取value1的值。
拜托,你能帮帮我吗?我尝试了很多方法,但我仍然是正则表达式的新手。
提前致谢。
答案 0 :(得分:0)
你想要这样的输出吗?
as5q
as
as5
然后,您可以尝试此regex
(?s)start.*?value1\s+(a[^\s]*)
Demo,,,其中目标值(value1
的值包含前缀“a
”)被捕获到group 1
(\1
)
在python
脚本中,它可能就像这样
import re
ss=""" copy&paste sample text in this area """
regx= re.compile(r'(?s)start.*?value1\s+(a[^\s]*)')
for m in regx.finditer(ss):
print(m.group(1))
答案 1 :(得分:-2)
my_text="""start
value1 as5q
value3 dd9w
type b
value6 dp1p
stop
start
value1 as
value2 er
type a
stop
start
value1 as5
value2 er2
type c
value4 dd1
stop"""
blocks = re.findall("(?:start)(.*?)(?:stop)",my_text,re.DOTALL)
这会使用非捕获组,非贪婪匹配运算符和re.DOTALL标志以匹配换行符