2010-04-25 71 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"