您所需要的正則表達式是:
"(?s)==(?!.*?==)([^(?:DDD)]*)"
下面是完整的代碼:
String[] sarr = {"aaa\n==test==\naaa\nbbb\naaa", "bbb\naaa==toast==cccdd\nb\nc",
"aaa\n==trick==\naaaDDDaaa\nbbb"};
Pattern pt = Pattern.compile("(?s)==(?!.*?==)([^(?:DDD)]*)");
for (String s : sarr) {
Matcher m = pt.matcher(s);
System.out.print("For input: [" + s + "] => ");
if (m.find())
System.out.println("Matched: [" + m.group(1) + ']');
else
System.out.println("Didn't Match");
}
OUTPUT:
For input: [aaa\n==test==\naaa\nbbb\naaa] => Matched: [\naaa\nbbb\naaa]
For input: [bbb\naaa==toast==cccdd\nb\nc] => Matched: [cccdd\nb\nc]
For input: [aaa\n==trick==\naaaDDDaaa\nbbb] => Matched: [\naaa]
請詳細說明與樣本問題行 –
你的要求是你可以匹配直到換行,不包括換行符。 – keyser
如果您可以告訴我們您想要匹配的輸入文字,會更好。之後我們可以使用正確的正則表達式。 – anubhava