2013-07-22 89 views
1

之間,我想兩線之間,例如搜索特定字符串的所有出現正則表達式查找特定字符串的所有出現兩條線

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'中的shell腳本中加入它 – swalkner

+0

你不太清楚。你希望匹配的單詞恰好在短語之後,還是剛剛在它們中間? – DontVoteMeDown

回答

2

嘗試組1:

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

FYI (?s)打開 「點匹配換行符」

+0

,這個工作;不幸的是,我使用的shell腳本egrep的,它不:'egrep的:重複的操作員操作invalid' – swalkner

+0

使用'-P'選項與egrep的,它激活Perl的正則表達式 – Bohemian

+0

:(似乎是OSX一個相當大的交易:在山獅沒有perl正則表達式:( – 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),並採取了只RE4

0

試試這個

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); 
相關問題