java - Java 中分隔符之间的多行文本匹配

标签 java regex multiline

如何在 Java 中匹配分隔符之间的多行文本?

通过示例最好地解释问题:

...
unimportant text
EndOfEntry
Key=Value
unimportant text
maybe a few lines of unimportant text
AnotherKey=AnotherValue
EndOfEntry
more unimportant text
...

在上面,我想匹配 Key=Value.*AnotherKey=AnotherValue 一起出现在一个条目中。我只是想知道该模式是否出现——我不需要替换任何东西。

但是,对于相同的期望匹配,如果给出多个条目,例如:

...
unimportant text
EndOfEntry
Key=Value
unimportant text
maybe a few lines of unimportant text
AnotherKey=NotMyValue
EndOfEntry
RandomKey=Value
unimportant text
maybe a few lines of unimportant text
AnotherKey=AnotherValue
EndOfEntry
more unimportant text
...

我不希望上面的内容匹配成功,因为我们在单个“条目”中看不到确切的 Key=Value 和 AnotherKey=AnotherValue 。相反,我们在第一个条目中看到 Key=Value,在第二个条目中看到 AnotherKey=AnotherValue。

我一直在尝试使用像这样的正则表达式(当然\S\s 可以替换为 Pattern 的 DOTALL 选项):

Key=Value[\S\s]*?AnotherKey=AnotherValue

但当然两者都匹配。我也尝试过:

Key=Value[^EndOfEntry]*?AnotherKey=AnotherValue

但这不起作用,因为这样就没有点了,而且我们根本不匹配换行符。

是否有一个正则表达式可以精确匹配我正在寻找的内容?首先剥离换行符或其他一些两步处理(我只是为了教育而试图避免)会简化事情吗?

最佳答案

您应该简单地使用:

\bKey=Value\b(?:(?!EndOfEntry).)*?\bAnotherKey=AnotherValue\b

(按照您在问题中的建议,使用 DOTALL 标志)。

现场实验 here on regex101 .

<小时/>

工作原理:

我基本上只是将您的 .* 替换为该表达式:((?!EndOfEntry).)*,它大致代表任何不' t 包含 EndOfEntry

此外,为了避免与 RandomKey=ValueAnotherKey=AnotherValue 对匹配,因为 RandomKey=Value 也会匹配 Key=Value(例如),我添加了另一个小调整:

我已经用 \b 包围了你的对(断言我们处于单词边界)(或 \s,对于任何空格字符),所以我们仅当整个单词匹配时才会有匹配。

<小时/>

这是一段 Java 代码,它使用我针对您的示例建议的正则表达式:

final Pattern pattern = Pattern.compile("\\bKey=Value\\b(?:(?!EndOfEntry).)*?\\bAnotherKey=AnotherValue\\b", Pattern.DOTALL);

final String invalid = "unimportant text\n" +
                "EndOfEntry\n" +
                "Key=Value\n" +
                "unimportant text\n" +
                "maybe a few lines of unimportant text\n" +
                "AnotherKey=NotMyValue\n" +
                "EndOfEntry\n" +
                "RandomKey=Value\n" +
                "unimportant text\n" +
                "maybe a few lines of unimportant text\n" +
                "AnotherKey=AnotherValue\n" +
                "EndOfEntry\n" +
                "more unimportant text";

final String valid = "unimportant text\n" +
                "EndOfEntry\n" +
                "Key=Value\n" +
                "unimportant text\n" +
                "maybe a few lines of unimportant text\n" +
                "AnotherKey=AnotherValue\n" +
                "EndOfEntry\n" +
                "more unimportant text";

System.out.println(pattern.matcher(invalid).find());
System.out.println(pattern.matcher(valid).find());

输出:

false
true

关于java - Java 中分隔符之间的多行文本匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25655017/

相关文章:

regex - 我网站上正则表达式特定的问题和搜索功能,处理断开的链接

android - 以编程方式创建多行 EditText

java - 正则表达式 : Multiline check problem

java - 通过 Jackson ObjectMapper 将参数化 url 反序列化为 java.net.URI 时出现 InvalidFormatException

java - 单击时递增 Int

python - 使用 python 从字符串中查找并替换标记的单词?

xcode - 在 Xcode 运行脚本 bin/sh 中使用 sed 查找和替换多行

java - 在 for 或 while 循环的 ActionListener 内添加组件 CodenameOne

java - Struts 中的 Controller

Java Scanner - 除非存在引号,否则用空格分隔?