2016-04-26 110 views
1

我有以下报告。一些有这样的:正则表达式只匹配第一个lookbehind

Symptom Correlation to Reflux 
Table 
Symptom Correlation to Reflux 
Table 
Reflux Symptom Index 
Table 

和一些有这样的:

Symptom Correlation to Reflux 
Table 
Reflux Symptom Index 
Table 

我要永远只能捕捉Symptom Correlation to RefluxReflux Symptom Index的表。

我怎样才能做一个积极的回顾后,只有比赛进行到第Symptom Correlation to Reflux和捕获匹配 - 我想与正回顾后

非贪婪的运营商内的表是不是有点像(不工作):

.*?(?<=Reflux Symptom Index)Symptom Correlation to Reflux 
+0

你有没有在你的正则表达式引擎可用正预测先行?你的正则表达式不考虑换行符,这可能是它不匹配的原因吗? – collapsar

+0

对不起。我使用java,所以我可以考虑换行符,我也可以使用积极lookahead –

回答

1

在Java中,你可以使用这个表达式负前瞻:

(?s)\bSymptom Correlation to Reflux\b((?:(?!Symptom Correlation to Reflux).)*?)\bReflux Symptom Index\b 

的Java代码:

Pattern p = Pattern.compile(
"(?s)\\bSymptom Correlation to Reflux\\b((?:(?!Symptom Correlation to Reflux).)*?)\\bReflux Symptom Index\\b"); 

table是捕获组#1

(?:(?!Symptom Correlation to Reflux).)*?可以是负预测先行断言,以确保我们不会在开始/结束的中间匹配另一个Symptom Correlation to Reflux

RegEx Demo

+1

很好。非常感谢 –

1

你可以申请这个表达式:

/(?<=\bSymptom Correlation to Reflux\b).*(?=\bReflux Symptom Index\b)/s 

它的Symptom Correlation to Reflux第一次出现之间的匹配,直到Reflux Symptom Index第一次出现。请注意与.匹配换行符(非默认值)的s匹配参数。

0

请尝试以下模式:

/(?<=\bSymptom Correlation to Reflux\b\n)(\S+)\s*(?=\bReflux Symptom Index\b)/g 

REGEX 101 DEMO.