之间,我想两线之间,例如搜索特定字符串的所有出现正则表达式查找特定字符串的所有出现两条线
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