Java 用于指定空字符串的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3164452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:01:29  来源:igfitidea点击:

Regex for specifying an empty string

javaregexstring

提问by prabha

I use a validator that requires a regex to be specified. In the case of validating against an empty string, I don't know how to generate such a regex. What regex can I use to match the empty string?

我使用需要指定正则表达式的验证器。在针对空字符串进行验证的情况下,我不知道如何生成这样的正则表达式。我可以使用什么正则表达式来匹配空字符串?

回答by Chowlett

I don't know about Java specifically, but ^$usually works (^matches only at the start of the string, $only at the end).

我不特别了解 Java,但^$通常有效(^仅在字符串的开头匹配,$仅在结尾匹配)。

回答by polygenelubricants

The regex ^$matches only empty strings (i.e. strings of length 0). Here ^and $are the beginning and end of the string anchors, respectively.

正则表达式^$只匹配空字符串(即长度为 0 的字符串)。这里^$分别是字符串锚点的开始和结束。

If you need to check if a string contains only whitespaces, you can use ^\s*$. Note that \sis the shorthand for the whitespace character class.

如果您需要检查字符串是否仅包含空格,您可以使用^\s*$. 请注意,这\s是空白字符类的简写。

Finally, in Java, matchesattempts to match against the entirestring, so you can omit the anchors should you choose to.

最后,在 Java 中,matches尝试匹配整个字符串,因此您可以选择省略锚点。

References

参考

API references

API 参考



Non-regex solution

非正则解决方案

You can also use String.isEmpty()to check if a string has length 0. If you want to see if a string contains only whitespace characters, then you can trim()it first and thencheck if it's isEmpty().

您还可以使用String.isEmpty()检查一个字符串的长度为0。如果你想看看字符串只包含空格字符,那么你可以trim()首先和然后检查它是否是isEmpty()

回答by neo_r23

For checking empty string i guess there is no need of regex itself... u Can check length of the string directly ..

为了检查空字符串,我想不需要正则表达式本身......你可以直接检查字符串的长度..

in many cases empty string and null checked together for extra precision.

在许多情况下,空字符串和空值一起检查以获得额外的精度。

like String.length >0 && String != null

像 String.length >0 && String != null

回答by David Abragimov

If you have to use regexp in Java for checking empty string you can use

如果您必须在 Java 中使用正则表达式来检查空字符串,您可以使用

 String testString = "";
 System.out.println(testString.matches(""));

or for checking if only white-spaces:

或检查是否只有空格:

String testString = "  ";        
testString.trim().matches("");

but anyway using

但无论如何使用

testString.isEmpty();
testString.trim().isEmpty();

should be match better from performance perspective.

从性能的角度来看,应该更好地匹配。

public class Main {

public static void main(String[] args) {

    String testString = "";

    long startTime = System.currentTimeMillis();
    for (int i =1; i <100000000; i++) {
        testString.matches("");
    }
    long endTime = System.currentTimeMillis();

    System.out.println("Total testString.matches execution time: " + (endTime-startTime) + "ms");


    startTime = System.currentTimeMillis();
    for (int i =1; i <100000000; i++) {
        testString.isEmpty();
    }
    endTime = System.currentTimeMillis();

    System.out.println("Total testString.isEmpty execution time: " + (endTime-startTime) + "ms");


}

}

}

Output:

输出:

C:\Java\java8\bin\java.exe
Total testString.matches execution time: 18776ms
Total testString.isEmpty execution time: 12ms