2016-06-30 3 views
0

단락 패턴을 일치 시키려고하는데 문제가 있습니다.정규식 일치 단락 패턴

[image.gif] 
some words, usually a few lines 

name 

emailaddress<mailto:[email protected]> 

내가 gif image<mailto: 사이에있는 모든 일치하는 시도했지만 이것은 내가 나쁜 결과를 얻을 의미 파일에 여러 번 발생합니다

패턴입니다.

나는 단락의 일반적인 레이아웃에 맞게 정규식을 사용하는 방법이 있나요이

(?<=\[image.gif\].*?(\[image.gif\])).*?(?=<mailto:) 

와 그것을 시도?

+0

당신 테스트를. 질문을 수정할 수 있습니까? – trincot

+0

느린'(? s) (? <= \ [image \ .gif \]). *? (? = sln

답변

1

"단락의 일반 레이아웃"에는 더 나은 정의가 필요합니다. 입력과 예상 출력의 부족을 감안할 때 여기서 원하는 것을 맞춰야합니다. 나는 또한 당신이 어떤 언어라도 받아 들일 것으로 추측하고 있습니다. 여기에 perl이 있습니다. 거의 익숙한 언어가 아닙니다.

가정되는 입력 :

do not match this line 
[image.gif] 
some words, usually a few lines 

Bobert McBobson 

emailaddress<mailto:[email protected]> 
don't match this line either 
[image.gif] 
another few words 
on another few lines 

Bobina Robertsdaughter 

emailaddress<mailto:[email protected]> 
this line is also not for matching 

예상 출력 :

[image.gif] 
some words, usually a few lines 

Bobert McBobson 

emailaddress<mailto:[email protected]> 
--- 
[image.gif] 
another few words 
on another few lines 

Bobina Robertsdaughter 

emailaddress<mailto:[email protected]> 

perl 사용 해결책 :

#!/usr/bin/perl -n007 

my $sep = ""; 
while (/(\[image\.gif\].*?<mailto:[^>]*>(\r)?\n)/gms) { 
    print $sep . $1; 
    $sep = "---$2\n"; 
} 

perl 정규식 언어 왕; 많은 사람들이 그게 다 좋은 것이라고 말합니다. 여기서는 -n007 옵션을 사용하여 각 파일의 전체 내용을 읽고 기본 변수로 코드를 실행하도록 지정합니다.

$sep은 두 번째 경기까지 분리 할 것이 없으므로 공백으로 시작합니다.

그런 다음 정규식과 일치하는 텍스트의 각 블록을 통해 루프 :

  • 는 리터럴 [image.gif]
  • 후 가능
  • 으로 다음 문자 <mailto:과 일치 할 때까지 계속해서 다음과 같이 약간의 내용과 일치 일치를 다음 >
  • 은 줄 바꿈을 캡처합니다 (DOS 줄 끝에 대한 선택적 지원 포함)
  • (see full regex explanation and example at regex101)

그런 다음 일치 항목을 인쇄하고 마침내 구분 기호를 세 개의 대시 및 줄 바꿈 (필요할 경우 DOS 줄 끝이 추가됨)으로 설정합니다.

이제 당신은 그것을 실행할 수 있습니다 :하여 Image.gif \]`하는 샘플 입력 발생하지 않습니다`에 대한

$ perl answer.pl input.txt 
[image.gif] 
some words, usually a few lines 

Bobert McBobson 

emailaddress<mailto:[email protected]> 
--- 
[image.gif] 
another few words 
on another few lines 

Bobina Robertsdaughter 

emailaddress<mailto:[email protected]>