regex

RegEx to allow anything except blanks or all spaces?


I want to allow anything and everything..

except blank entries (NULL, zero characters, whatever you want to call it) and also blank spaces of any length should not be allowed.

This is essentially what I would do with a TRIM() function if I were coding in a language, but I have a need to do this with only regex.

Thank you!


Solution

  • How about this regex:

    ^(?! +$).+$
    

    This will make sure that:

    1. Input is not empty
    2. Input doesn't only have 1+ spaces
    3. Spaces mixed with non-space characters are allowed
    4. (?! +$) is a negative lookahead that fails the match when get 1+ spaces right after start position till end.

    RegEx Demo