23

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?

3
  • 4
    I dont see why you need a regex the check this. Commented Jul 2, 2010 at 9:16
  • 2
    prabha commented below that a validator required it to be a regex - this should have been part of the question from the start, not a comment! Commented Jul 2, 2010 at 10:35
  • Edited the question to mention the regex requirement. Commented Apr 30, 2013 at 3:17

4 Answers 4

51

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

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

Finally, in Java, matches attempts to match against the entire string, so you can omit the anchors should you choose to.

References

API references


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 then check if it's isEmpty().

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the info.The regex solution worked :). I use a validator and that required a regex to be specified.Anyways thanks a lot
5

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

Comments

2

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

testString.matches("")

please see examples:

 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 better from performance perspective.

   public static void main(String[] args) {

        String testString = "";

        long startTime = System.currentTimeMillis();
        for (int i =1; i <100000000; i++) {

            // 50% of testStrings are empty.
            if ((int)Math.round( Math.random()) == 0) {
                testString = "";
            } else {
                testString = "abcd";
            }

             if (!testString.isEmpty()){
                testString.matches("");
            }

        }
        long endTime = System.currentTimeMillis();


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


        startTime = System.currentTimeMillis();

        for (int i =1; i <100000000; i++) {
            // 50% of testStrings are empty.
            if ((int)Math.round( Math.random()) == 0) {
                testString = "";
            } else {
                testString = "abcd";
            }

            testString.matches("");


        }

        endTime = System.currentTimeMillis();

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

    }

Output:

C:\Java\jdk1.8.0_221\bin\java.exe 
Total testString.empty() execution time: 11023ms
Total testString.matches execution time: 17831ms

2 Comments

That's compiler optimization. To avoid that and actually compare apples & apples, print each result in the loop and measure, isEmpty is only a little better.
@Anand Rockzz good point, I have updated code with more realistic example. probably still not exact apples & apples but I see 50% better performance.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.