java - 选择文本中的所有模式

标签 java regex

我想显示文本中的所有行,但目前我只显示第二行。错误出现在我的 rowRegex :( 中。 我将非常感谢任何帮助。

我的代码:

package test.jpa;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class FireTest {

    public static void main(String[] arStrings) throws Exception {

        String str = "*PureNG*#"
                   + "\"*Part*\":https://pure1.pdf** \"Part1\":https://pure2.pdf** \"Part2\":https://pure3.pdf** \"Part3\":https://pure4.pdf#"
                   + "\"*Fakt*\":https://pure5.pdf** \"Fakt1\":https://pure5.pdf** \"Fakt2\":https://pure6.pdf#"
                   + " \"*WZ* _(wydanie)_\":https://pure7.pdf#"
                   + " \"*ZA*\":https://pure8.pdf** \"PA\":https://pure9.pdf#"
                   + " \"*Close*\":https://pure11.pdf** \"Close1\":https://pure12.pdf#"
                   + " \"*Stany*\":https://pure13.pdf</text><version>7</version><author id=\"1\" name=\"UserName LastName Admin\"/><comments></comments> <created_on>2015-11-26T15:08:26Z</created_on><updated_on>2015-11-30T15:44:00Z</updated_on></wiki_page>";

        String rowRegex = "(#.*?#|\\Z)";
        Pattern patternRow = Pattern.compile(rowRegex);
        Matcher matcher = patternRow.matcher(str);

        while(matcher.find()) {
            System.out.println("Finded: " + matcher.group());
        }
    }
}

目前这就是我的结果:

Finded: #"*Part*":https://pure1.pdf** "Part1":https://pure2.pdf** "Part2":https://pure3.pdf** "Part3":https://pure4.pdf#
Finded: # "*WZ* _(wydanie)_":https://pure7.pdf#
Finded: # "*Close*":https://pure11.pdf** "Close1":https://pure12.pdf#
Finded: 

但我想得到以下输出:

Finded: #"*Part*":https://pure1.pdf** "Part1":https://pure2.pdf** "Part2":https://pure3.pdf** "Part3":https://pure4.pdf
Finded: #"*Fakt*":https://pure5.pdf** "Fakt1\":https://pure5.pdf** "Fakt2":https://pure6.pdf
Finded: # "*WZ* _(wydanie)_":https://pure7.pdf
Finded: # "*ZA*":https://pure8.pdf** "PA":https://pure9.pdf
Finded: # "*Close*":https://pure11.pdf** "Close1":https://pure12.pdf
Finded: # *Stany*\":https://pure13.pdf</text><version>7</version><author id=\"1\" name=\"UserName LastName Admin\"/><comments></comments><created_on>2015-11-26T15:08:26Z< created_on><updated_on>2015-11-30T15:44:00Z</updated_on></wiki_page>"

最佳答案

你的正则表达式必须看起来像

String rowRegex = "#[^#]++";

请参阅IDEONE demo

# 匹配文字 #[^#]++ 将匹配 #< 以外的 1 个或多个符号(所有格)。

如果您需要丢弃第一个#,请使用捕获组#([^#]++)并使用匹配器访问第一个组。组(1)

关于java - 选择文本中的所有模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34167866/

相关文章:

java - 使用放心的java对不同的请求主体进行数据驱动测试的最佳方法

regex - 谷歌应用脚​​本正则表达式

regex - 从一系列输入生成正则表达式

java - 这个 XML/XSD 有什么问题?

java - 我如何在 libgdx 中设置固定的 shaperenderer 旋转?

java - 将退出代码从 Java 返回到 PL/SQL

java - 在 Java 中调用 Oracle Sproc 时出现问题(使用 Spring 3.0)

node.js - Express:逻辑或路由中

javascript - 如何从字符串数组中以任意顺序匹配并突出显示所有术语?

javascript - 使用 jQuery 进行时间/日期识别