Search code examples
phpregexpcre

Detect non-empty string with regexp?


How do I write a PHP regexp which detects that a string is not empty?

Empty means, in this case 0 characters. A single space, or a single newline counts as not empty, for instance.

(It has to be regexp suitable for preg_match(), since I have a lookup table with various regexps and don't want to handle this case in any special way, it would complicate the code to not use a regexp here.)


I also can not use any regex modifiers such as "s" outside the // for sad reasons.


Solution

  • /[\s\S]/
    

    matches any character, even if you can't use the /s modifier.

    You don't need a quantifier (+) because if one character matches, then the condition is already fulfilled.