2013-07-22 24 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); 
相关问题