8

i want to match a string that can have any type of whitespace chars (specifically I am using PHP). or any way to tell if a string is empty or just has whitespace will also help!

7 Answers 7

20

You don't need regular expressions for that, just use:

if ( Trim ( $str ) === '' ) echo 'empty string';
5
  • 2
    +1: This will be faster than compiling a regex, and more readable/maintainable.
    – Ben S
    Commented Dec 2, 2009 at 16:21
  • 1
    Thats right, but may not suit for all cases. eg. Dynamically generating pattern and using only once preg_match($pattern, $str) Commented Dec 2, 2009 at 16:24
  • @Dominic: "Trim" works just as fine as "trim", it's just a matter of coding style :) Commented Dec 2, 2009 at 16:29
  • 3
    And (case-insensitivity aside) beginning a function with an uppercase letter is a style almost all PHP developers would avoid
    – Ben James
    Commented Dec 2, 2009 at 16:41
  • Like I said, it's a matter of style. I guess I brought that from my .NET days ... Commented Dec 2, 2009 at 17:57
9

Checking the length of the trimmed string, or comparing the trimmed string to an empty string is probably the fastest and easiest to read, but there are some cases where you can't use that (for example, when using a framework for validation which only takes a regex).

Since no one else has actually posted a working regex yet...

if (preg_match('/\S/', $text)) {
    // string has non-whitespace
}

or

if (preg_match('/^\s*$/', $text)) {
    // string is empty or has only whitespace
}
2
if (preg_match('^[\s]*$', $text)) {
    //empty
} 
else {
    //has stuff
}

but you can also do

if ( trim($text) === '' ) {
    //empty
}

Edit: updated regex to match a truly empty string - per nickf (thanks!)

2
  • The first one fails on, ironically, an empty string. Change to a * match and add some delimiters and you're right.
    – nickf
    Commented Dec 2, 2009 at 16:28
  • 2
    Still no delimiters. Your regular expression needs them.
    – Tadeck
    Commented Apr 21, 2012 at 0:23
1
if(preg_match('^[\s]*[\s]*$', $text)) {
    echo 'Empty or full of whitespaces';
}

^[\s]* means the text must start with zero or more whitespace and [\s]*$ means must end with zero or more whitespace, since the expressions are "zero or more", it also matches null strings.

4
  • 1. there's no matching parentheses in that regex. 2. there's no delimiters either. 3. you might want to explain whether this checks for an empty or a non-empty string.
    – nickf
    Commented Dec 2, 2009 at 16:27
  • 2
    I don't understand all the upvotes for this answer. It is much too verbose. The following regex does exactly the same: ^\s*$. You don't need to put the \s in a character class, and don't need to add it twice. -1 from me, sorry.
    – Bart Kiers
    Commented Dec 2, 2009 at 18:38
  • When I first write, it was exactly what you say. I have changed it for clarity and nickf's explanation request. Commented Dec 3, 2009 at 4:24
  • 1
    Well, IMO, you didn´t change it for the better.
    – Bart Kiers
    Commented Dec 3, 2009 at 11:14
0

You don't really need a regex

if($str == '') { /* empty string */ }
elseif(trim($str) == '') { /* string of whitespace */ }
else { /* string of non-whitespace */ }
0

The following regex checks via lookahead and lookbehind assertion, if the string contains whitespace at the beginning or at the end or if the string is empty or contains only whitespace:

/^(?!\s).+(?<!\s)$/i

invalid (inside "):

""
" "
" test"
"test "

valid (inside "):

"t"
"test"
"test1 test2"
-1

Expression is \A\s*+\Z

1
  • What about \z? From the documentation \Z matches at end of string, or before newline at the end; where as \z matches at end of string only.
    – PP.
    Commented Dec 9, 2009 at 14:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.