正则表达式查找文本文件的特定部分

时间:2013-04-13 15:07:48

标签: regex

我想使用正则表达式从文本文件中找到一个部分。我的文件如下:

This is general text section that I don't want.
HEADER:ABC1
This is section text under header
More text to follow 
Additional text to follow
HEADER:XYZ2
This is section text under header
More text to follow 
Additional text to follow
HEADER:KHJ3
This is section text under header 
A match text will look like this A:86::ABC

现在,如果部分文本包含匹配HEADER,我想要检索最多A:86::ABC的所有部分文字。结果文本将是

(HEADER:KHJ3
This is section text under header 
A match text will look like this A:86::ABC). 

我感谢任何帮助。我正在使用python,匹配部分可以在文件中不止一个。这也是一个多行文件。

1 个答案:

答案 0 :(得分:1)

regex = re.compile(".*(HEADER.*$.*A:86::ABC)", re.MULTILINE|re.DOTALL)
>>> regex.findall(string)
[u'HEADER:KHJ3\nThis is section text under header \nA match text will look like this A:86::ABC']

希望这会有所帮助。 对于2次捕获,请使用".*(HEADER.*$)(.*A:86::ABC)"

相关问题