0

我想在两行之间搜索所有出现的特定字符串,例如

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

正则表达式如何在“这是开始”和“这是结束”之间找到“注意”?有没有办法做到这一点?

4

3 回答 3

2

尝试此正则表达式的第 1 组:

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

仅供参考(?s)打开“点匹配换行符”

于 2013-07-22T12:27:47.380 回答
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

于 2013-07-22T12:29:38.587 回答
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);
于 2013-07-22T12:46:50.707 回答