Regular expression does not match newline obtained from Formatter object

情到浓时终转凉″ 提交于 2019-11-27 14:39:00

问题


I cannot match a String containing newlines when the newline is obtained by using %n in Formatter object or String.format(). Please have a look at the following program:

public class RegExTest {

  public static void main(String[] args) {
    String input1 = String.format("Hallo\nnext line");
    String input2 = String.format("Hallo%nnext line");
    String pattern = ".*[\n\r].*";
    System.out.println(input1+": "+input1.matches(pattern));
    System.out.println(input2+": "+input2.matches(pattern));
  }

}

and its output:

Hallo
next line: true
Hallo
next line: false

What is going on here? Why doesn't the second string match?

Java version is 1.6.0_21.


回答1:


You can set the Pattern.DOTALL flag to make . match newlines, as default it doesn't. It is done with the (?s) notation. So, this regex does what you want:

    String pattern = "(?s).*[\n\r].*";



回答2:


On Windows, in Java, \n is LF, \r is CR and %n is CRLF. Your pattern does not match the latter.

As of Java 8, you can now use \R in regular expressions to match any end-of-line sequence.

Linebreak matcher

\R Any Unicode linebreak sequence, is equivalent to \u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]

Example:

String pattern = ".*\\R.*";
String.format("Hallo\nnext line").matches(pattern); // true
String.format("Hallo%nnext line").matches(pattern); // true
String.format("Hallo same line").matches(pattern); // false


来源:https://stackoverflow.com/questions/11644100/regular-expression-does-not-match-newline-obtained-from-formatter-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!