2013-04-13 5 views
-5

정규식을 사용하여 텍스트 파일에서 섹션을 찾고 싶습니다. 나는 아래와 같은 파일이 있습니다 :정규식을 사용하여 텍스트 파일의 특정 섹션을 찾습니다.

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 

지금, 나는 섹션 텍스트가 일치 A:86::ABC이 포함 된 경우 HEADER 모든 섹션의 텍스트를 검색 할 수 있습니다. 결과 텍스트는

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

입니다. 감사합니다. 나는 파이썬을 사용하고 있으며 일치하는 부분은 하나 이상의 파일에있을 수 있습니다. 또한 다중 행 파일입니다.

+0

어떤 프로그래밍 언어를 사용하십니까? – user4035

답변

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)"

+0

감사! 제발 당신이 정규식을 설명 요청할 수 있습니다. – user2277675

관련 문제