Q: Regex - Match String That Does Not Contain Only Spaces I am looking for a regex expression that will return true if the string does not contain only spaces. My attempt so far is ^[^ \s]*$ but it does not pick up on strings that contains multiple words.
I want to be clear that I am trying to match a string that does not contain only spaces.
Test case:
'apple'                true
'apple banana'         true
'apple banana orange'  true
' apple banana'        true
'  apple banana'       true
' '                    false
'  '                   false
'   '                  false


A: You might very well get along with:
^(?!\s+$).+

See a demo on regex101.com.

Broken down, this says:

^         # start of the line / string
(?!\s+$)  # neg. lookahead, making sure there are not only whitespaces
.+        # at least one character, possibly more


A: *

*Use \S to match non-space characters

*Allow spaces before and after a non-space


For example:
^\s*\S.*$


A: Use a look ahead for a non-space:
^(?=.*\S).*


The good thing about look aheads is you can stack them up to combine unrelated assertions that might be hard to write as a straight regex. For example, suppose we wanted to add an assertion that there were no numbers:
^(?=.*\S)(?!.*\d).*

It would be awkward to write that as a simple regex, plus this is about as readable as regex gets - the look aheads are kept separate from each other and the main regex.

A: The simplest regex to do that is:
\S

It will match any string that contains a non-whitespace character.
