简体   繁体   English

用于多行文本的Java Regex

[英]Java Regex for multiline text

I need to match a string against a regex in Java. 我需要在Java中匹配一个字符串与正则表达式。 The string is multiline and therefore contains multiple \\n like the followings 该字符串是多行的,因此包含多个\\n ,如下所示

String text = "abcde\n"
        + "fghij\n"
        + "klmno\n";
String regex = "\\S*";
System.out.println(text.matches(regex));

I only want to match whether the text contains at least a non-whitespace character. 我只想匹配文本是否至少包含非空白字符。 The output is false. 输出为false。 I have also tried \\\\S*(\\n)* for the regex, which also returns false. 我也尝试过\\\\S*(\\n)*用于正则表达式,它也返回false。

In the real program, both the text and regex are not hard-coded. 在真实程序中,文本和正则表达式都不是硬编码的。 What is the right regex to check is a multiline string contains any non-whitespace character? 要检查的正确的正则表达式是多行字符串包含任何非空白字符?

The problem is not to do with the multi lines, directly. 问题与直线无关。 It is that matches matches the whole string, not just a part of it. matches匹配整个字符串,而不仅仅是它的一部分。

If you want to check for at least one non-whitespace character, use: 如果要检查至少一个非空白字符,请使用:

"\\s*\\S[\\s\\S]*"

Which means 意思是

  • Zero or more whitespace characters at the start of the string 字符串开头的零个或多个空格字符
  • One non-whitespace character 一个非空白字符
  • Zero or more other characters (whitespace or non-whitespace) up to the end of the string 零个或多个其他字符(空格或非空格)直到字符串的末尾

If you just want to check whether there is at least one non white space character in the string, you can just trim the text and check the size without involving regex at all. 如果您只想检查字符串中是否至少有一个非空格字符,您可以修剪文本并检查大小而不涉及正则表达式。

String text = "abcde\n"
    + "fghij\n"
    + "klmno\n";
if (!text.trim().isEmpty()){
    //your logic here
}

If you really want to use regex, you can use a simple regex like below. 如果你真的想使用正则表达式,你可以使用如下的简单正则表达式。

String text = "abcde\n"
    + "fghij\n"
    + "klmno\n";
String regex = ".*\\S+.*";
Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
Matcher matcher = pattern.matcher(string);
if (matcher.find()){
    // your logic here
}

Using String.matches() 使用String.matches()

!text.matches("\\s*")

Check if the input text consist solely of whitespace characters (this includes newlines), invert the match result with ! 检查输入文本是否仅包含空格字符(包括换行符),将匹配结果反转为!

Using Matcher.find() 使用Matcher.find()

Pattern regexp = Pattern.compile("\\S");
regexp.matcher(text).find()

Will search for the first non-whitespace character, which is more efficient as it will stop on the first match and also uses a pre-compiled pattern. 将搜索第一个非空白字符,这将更有效,因为它将在第一个匹配时停止并且还使用预编译模式。

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

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