我有一个文本文件,如: -
car - superb
company - skoda simply clever
manufactured - 1990
car - sclass
company - mercedes the best or nothing
manufactured - 1990
car - rapid
company - skoda simply clever
manufactured - 1990
如果用户raw_inputs公司名称'koda',那么它必须提供2个细节,因为上面的文本文件有2辆车: -
superb
skoda simply clever
1990
rapid
skoda simply clever
1990
我如何使用正则表达式解决上述问题,我搜索了整个'SO',但没有得到我的问题。请帮我解决一下!
提前致谢!
答案 0 :(得分:0)
car\s+-(?:(?!\bcar\b)[\s\S])*skoda(?:(?!\bcar\b)[\s\S])*
你可以使用它。这将给你想要的部分。参见演示。
https://regex101.com/r/pT4tM5/14
x="""car - superb
company - skoda simply clever
manufactured - 1990
car - sclass
company - mercedes the best or nothing
manufactured - 1990
car - rapid
company - skoda simply clever
manufactured - 1990"""
temp="skoda"
print re.findall(r"car\s+-(?:(?!\bcar\b)[\s\S])*"+temp+r"(?:(?!\bcar\b)[\s\S])*",x)