Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java正则表达式程序未按预期工作_Java_Regex_Multiline - Fatal编程技术网

Java正则表达式程序未按预期工作

Java正则表达式程序未按预期工作,java,regex,multiline,Java,Regex,Multiline,在运行这个程序时 import java.util.regex.*; public class PatternExample { public static final String numbers = "1\n22\n333\n4444\n55555\n666666\n7777777\n88888888\n999999999\n0000000000"; public static void main(String[] args) { Pattern patt

在运行这个程序时

import java.util.regex.*;

public class PatternExample {
    public static final String numbers = "1\n22\n333\n4444\n55555\n666666\n7777777\n88888888\n999999999\n0000000000";

    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("9.*", Pattern.MULTILINE);
        Matcher matcher = pattern.matcher(numbers);
        while (matcher.find()) {
            System.out.print("Start index: " + matcher.start());
            System.out.print(" End index: " + matcher.end() + " ");
            System.out.println(matcher.group());
        }
    }
}
输出为

Start index: 44 End index: 53 999999999
我希望输出包括零,因为
模式.MULTILINE

我应该怎么做才能包含零?

您需要添加
DOTALL
标志(并且不需要只适用于
^
$
行为的
多行
标志):

这是在

除非指定了
DOTALL
标志,否则正则表达式将匹配除行终止符以外的任何字符


您正在查找
模式。DOTALL
,请使用以下命令:

Pattern pattern = Pattern.compile("9.*", Pattern.DOTALL);

还有
模式。此处不需要多行
,因为您没有使用任何开始
^
和结束
$
锚定。

您需要添加标志
模式.DOTALL
。默认情况下,

Pattern pattern = Pattern.compile("9.*", Pattern.DOTALL);