2013-07-22 2 views
1

두 줄 사이에있는 특정 문자열의 모든 항목을 검색하고 싶습니다.두 줄 사이에서 특정 문자열의 모든 항목을 찾으려면 Regex

some line nobody is interested in 
this is the beginning 
this is of no interest 
attention 
not interesting 
this is the ending 

어떻게 정규식은 "이것은 시작에 불과하다"와 "이것이 끝은"사이의 "주의"를 찾아 같을 것이다? 그것을 성취 할 수있는 방법이 있습니까? 이 정규식의

+2

그것은 것 좋은 당신이 원하는 정규식 엔진을 especify합니다. – DontVoteMeDown

+0

쉘 스크립트에서'bash'를 포함시키고 싶습니다. – swalkner

+0

당신은 분명하지 않습니다. 일치하는 단어가 문구 바로 다음에 있고 그 직전에 나타나기를 원하거나 그 중간에 있어야합니다. – DontVoteMeDown

답변

2

보십시오 그룹 1 :

(?s)this is the beginning.*?(attention).*?this is the ending 

참고에 (?s) 차례 "점은 일치 개행"

+0

이 있습니다. 불행히도 egrep을 사용하는 나의 셸 스크립트에서, 그것은하지 못한다 : egrep : repetition-operand operand invalid' – swalkner

+0

perrep-regexp – Bohemian

+0

을 활성화시키는 egrep과 함께'-P' 옵션을 사용한다. (OSX에서 상당히 큰 것 같다. : 거기에 마운틴 라이온에 perl-regex : ( – swalkner

0

당신이

bool foundStart = false; 
for line in lines{ 
    if (line == "this is the beginning") 
     foundstart = true; 
    else if(line == "this is the ending") 
     foundstart = false; //set to false if could come beginning again 
     break; //or break directly 
    else if (foundstart && line == interestingpattern) 
     interesting_lines.Add(line); 
} 

또는 정규식과 일치 할 필요는 어떤 패턴 교환 "==" "흥미로운"현상이 한 번만 필요하면 다음과 같이하십시오 :

re1='.*?' # Non-greedy match on filler 
re2='(start)' # Word 1 //exchange to your pattern for start 
re3='.*?' # Non-greedy match on filler 
re4='(interesting)' # Word 2/Exchange to your pattern for interesting 
re5='.*?' # Non-greedy match on filler 
re6='(end)' # Word 3 // exchange to your ending pattern 

다음 시도 (re1+re2+re3+re4+re5+re6)를 컴파일 만

0

RE4 꺼내이

var test = "some line nobody is interested in this is the beginning this is of no interest attention not interesting this is the ending"; 

var testRE = (test.match("this is the beginning (.*) this is the endin")); 
alert(testRE[1].match(/attention/g).length); 
관련 문제