之間,我想兩線之間,例如搜索特定字符串的所有出現正則表達式查找特定字符串的所有出現兩條線
some line nobody is interested in
this is the beginning
this is of no interest
attention
not interesting
this is the ending
正則表達式如何在「這是開始」和「這是結束」之間尋找「注意」?有沒有辦法實現這一點?此正則表達式的
之間,我想兩線之間,例如搜索特定字符串的所有出現正則表達式查找特定字符串的所有出現兩條線
some line nobody is interested in
this is the beginning
this is of no interest
attention
not interesting
this is the ending
正則表達式如何在「這是開始」和「這是結束」之間尋找「注意」?有沒有辦法實現這一點?此正則表達式的
交易所"=="
你需要匹配
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
試試這個
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);
它會很好,如果你especify你想要的正則表達式引擎。 – DontVoteMeDown
我想在'bash'中的shell腳本中加入它 – swalkner
你不太清楚。你希望匹配的單詞恰好在短語之後,還是剛剛在它們中間? – DontVoteMeDown