Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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/9/git/22.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 - Fatal编程技术网

Java 正则表达式模式在字符串的每一行中查找整数

Java 正则表达式模式在字符串的每一行中查找整数,java,regex,Java,Regex,我这里有一个模式,在逗号后找到整数 我的问题是,我的返回值在新行中,因此该模式仅在新行中有效。我该如何解决这个问题?我想让它找出每一行的模式 感谢所有帮助: url = new URL("https://test.com"); con = url.openConnection(); is = con.getInputStream(); br = new BufferedReader(new InputStreamReader(is)); while ((line = br.readLine()

我这里有一个模式,在逗号后找到整数

我的问题是,我的返回值在新行中,因此该模式仅在新行中有效。我该如何解决这个问题?我想让它找出每一行的模式

感谢所有帮助:

url = new URL("https://test.com");
con = url.openConnection();
is = con.getInputStream();
br = new BufferedReader(new InputStreamReader(is));

while ((line = br.readLine()) != null) {
    String responseData = line;
    System.out.println(responseData);
}

pattern = "(?<=,)\\d+";
pr = Pattern.compile(pattern);
match = pr.matcher(responseData); // String responseData

System.out.println();

while (match.find()) {
    System.out.println("Found: " + match.group());
}
这是打印件:

Found: 0
Found: 0
Found: 0
您可以在编译正则表达式时使用该选项:

pattern = "(?<=,)\\d+";
pr = Pattern.compile(pattern, Pattern.MULTILINE);

pattern=“(?问题在于生成字符串,您只分配了
BufferedReader
中的最后一行:

responseData = line;
如果在尝试匹配之前打印
responseData
,您将看到它只有一行,而不是您所期望的

由于您正在使用
System.out.println
打印缓冲区的内容,因此您确实看到了整个结果,但保存到
responseData
的内容实际上是最后一行

您应该使用
StringBuilder
构建整个字符串:

StringBuilder str = new StringBuilder();
while ((line = br.readLine()) != null) {
    str.append(line);
}
responseData = str.toString();
// now responseData contains the whole String, as you expected

提示:使用调试器,它将使您更好地理解代码,并帮助您更快地发现错误。

显示示例输入和预期输出。您可能需要
DOT\u ALL
选项。如果没有示例输入和output@TheLostMind我更新了我的帖子。谢谢。如果文章末尾是
\n
行,应该行。@MarounMaroun它不行。我的代码只匹配第一行,而不匹配其余的。sandepsing。就像@MarounMaroun说的,你的代码应该行。。你的正则表达式很好,恐怕我不能对你的输入字符串说同样的话。是一个字符串还是多个字符串?我的想法是正确的,直到我读到打印输出。假设它是copy-pasted,有3个0,这意味着第一行仍然在resposneData中。对此有何解释?@MichaëlBenjaminSaerens我认为这是一个输入错误,最后找到的必须是3,而不是0。当您匹配
^
$
时,此标志是相关的。
StringBuilder str = new StringBuilder();
while ((line = br.readLine()) != null) {
    str.append(line);
}
responseData = str.toString();
// now responseData contains the whole String, as you expected