简体   繁体   English

python多行正则表达式findall

[英]python multiline regex findall

I'm trying to find multiple matches across multiple lines of text with a delimiter to stop the search using regex in python... my query works well for what I'm trying to accomplish if what I need is all on the same line: re.findall('([a-zA-Z]{3}\\d-[aAeE][rRsS]\\d.*), output)我正在尝试使用分隔符在多行文本中找到多个匹配项,以在 python 中使用正则表达式停止搜索......如果我需要的内容都在同一行,我的查询很适合我要完成的任务: re.findall('([a-zA-Z]{3}\\d-[aAeE][rRsS]\\d.*), 输出)

the problem is, sometimes the additional data I'm trying to capture doesn't fit on the same line and goes to the next... is there a way to set the pattern match to stop if it either finds the next match or hits a delimiter (= in this case)?问题是,有时我试图捕获的附加数据不适合同一行并转到下一行......是否有办法将模式匹配设置为在找到下一个匹配项或命中时停止分隔符(= 在这种情况下)? Simplified example with two matches below, and I need the ability to capture both...下面有两个匹配项的简化示例,我需要能够同时捕获...

Example例子

Port Id Description
3/2/4 Part of aggregate interface lag-4. Next device in path sea1-as2
lag-4, sea1-as2 3/1/2.

It seems that all you have to do is to add [\\s\\S]*?看来你所要做的就是添加[\\s\\S]*? to capture whatever coming in the next line and include the expected stops , | .捕获下一行中的任何内容并包括预期的停靠点, | . , | . to stop the match.停止比赛。 Note that it is important to make [\\s\\S]*?请注意,使[\\s\\S]*? lazy, otherwise, it will match the whole thing.懒惰,否则,它将匹配整个事情。

print(re.findall(r'([a-zA-Z]{3}\d-[aAeE][rRsS]\d[\s\S]*?\d)(?:,|\.)', output))

output输出

['sea1-as2 lag-4', 'sea1-as2 3/1/2']

You mentioned [a-zA-Z] and [aAeE][rRsS] .你提到了[a-zA-Z][aAeE][rRsS] There are several ways to set re.IGNORECASE so that [ae][rs] would suffice.有几种方法可以设置re.IGNORECASE,这样[ae][rs]就足够了。

You didn't make it clear if you're using re.MULTILINE or if you're deleting newlines before evaluating the regex.您没有说清楚是使用re.MULTILINE还是在评估正则表达式之前删除换行符。 You end with .* which could trivially become你以.*结尾,这可能会变成

[^=]*

if you want everything up to the = delimiter.如果你想要一切都到=分隔符。

Alternatively, before evaluating the regex you could split on \\n newline and = equal, so you hand in appropriate size chunks for evaluation.或者,在评估正则表达式之前,您可以在\\n换行符和=相等上拆分,因此您可以提交适当大小的块进行评估。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM