2010-04-25 68 views

回答

1

首先,exampledepot.com是一個非常糟糕的網站:從來沒有有史以來假設在那裏發現任何「真相」。

在正則表達式中,$從不匹配一個字符,它匹配一個位置。在(?m)模式下,它匹配換行符之前的「空字符串」或字符串的結尾。因此,給定字符串"abc\r\ndef"正則表達式".*abc$.*"不匹配,因爲\r\n不存在於您的正則表達式中。 $匹配c\r之間的位置。

你應該做的是這樣的:

System.out.println("abc\r\ndef".matches(".*abc$\r\n.*"));  // false 
System.out.println("abc\r\ndef".matches("(?m).*abc$\r\n.*")); // true 
0

我不熟悉的社區維基是如何工作的,但隨時如果認爲有用的使用示例。

System.out.println(
     Pattern.matches("(?m)^abc$\n^def$", "abc\ndef") 
    ); // prints "true" 

    System.out.println(
     Pattern.matches("(?sm)^abc$.^def$", "abc\ndef") 
    ); // prints "true"