简体   繁体   English

检查所有行是否与Java中的regex模式匹配

[英]Check that all lines match regex pattern in Java

How to check that all lines match regex pattern in Java. 如何在Java中检查所有行是否与regex模式匹配。

I mean that I be able to split lines myself in while loop. 我的意思是我能够分割线自己在while循环。 But is there any library or standard API, which implement this functionality? 但是有没有实现此功能的库或标准API?

UPDATE This is Ruby solution: 更新这是Ruby解决方案:

if text =~ /PATTERN/

Here's a utility method using Guava that returns true if every line in the supplied text matches the supplied pattern: 这是一个使用Guava的实用程序方法,如果提供的文本中的每一行都与提供的模式匹配,则返回true:

public static boolean matchEachLine(String text, Pattern pattern){
    return FluentIterable.from(Splitter.on('\n').split(text))
                         .filter(Predicates.not(Predicates.contains(pattern)))
                         .isEmpty();
}

There is no standard API functionality I know of to do this, however, something like this is easy enough: 我知道没有标准的API功能,但是,这样的事情很容易:

string.matches("(What you want to match(\r?\n|$))*+")

Usage: 用法:

String string = "This is a string\nThis is a string\nThis is a string";
System.out.println(string.matches("(This is a string(\r?\n|$))*+"));

\\r?\\n covers the most common new-lines. \\r?\\n涵盖了最常见的新线。
$ is end of string. $是字符串的结尾。
(\\r?\\n|$) is a new-line or the end of string. (\\r?\\n|$)是换行符或字符串结尾。
*+ is zero or more - but this is a possessive qualifier . *+为零或更多 - 但这是一个占有性限定符

So the whole thing basically checks that every line matches This is a string . 所以整个事情基本上检查每一行匹配This is a string

If you want it in a function: 如果你想在一个函数中:

boolean allLinesMatch(String string, String regex)
{
  return string.matches("(" + regex + "(\r?\n|$))*+");
}

Java regex reference . Java正则表达式参考

Prime example of why you need a possessive qualifier: 为什么需要占有资格的主要例子:

If you take the string This is a string. 如果你取字符串This is a string. repeated a few times (34 times to be exact) but have the last string be This is a string.s (won't match the regex) and have What you want to match be .* .* .*\\\\. 重复了几次(确切地说是34次),但是最后一个字符串是This is a string.s字符串。(与正则表达式不匹配)并且What you want to match.* .* .*\\\\. , you end up waiting a quite while with * . 你最后用*等了很*

* example - runtime on my machine - more than a few hours , after which I stopped it. *示例 - 在我的机器上运行 - 超过几个小时 ,之后我停止了它。

*+ example - runtime on my machine - much less than a second . *+示例 - 我的机器上的运行时间 - 远不到一秒钟

See Catastrophic Backtracking for more information. 有关详细信息,请参阅灾难性回溯

This is one I use 这是我用的

public static boolean multilineMatches(final String regex, final String text) {
    final Matcher m = Pattern.compile("^(.*)$", Pattern.MULTILINE).matcher(text);
    final Pattern p = Pattern.compile(regex);
    while(m.find()) {
        if (!p.matcher(m.group()).find()) {
            return false;
        }
    }
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM