49

I am trying to create a regex that will return false if the String pattern contains whitespace or is empty. So far I have this

[^\s] 

I think that will make sure the string does not contain whitespace but I am unsure of how to also check to make sure it is not empty. Any help would be appreciated.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
medium
  • 4,136
  • 16
  • 55
  • 66

7 Answers7

67

In my understanding you want to match a non-blank and non-empty string, so the top answer is doing the opposite. I suggest:

(.|\s)*\S(.|\s)*

This matches any string containing at least one non-whitespace character (the \S in the middle). It can be preceded and followed by anything, any character or whitespace sequence (including new lines): (.|\s)*.

You can try it with explanation on regex101.

Tony
  • 1,057
  • 8
  • 14
  • 1
    After trying all of the varying answers I've found for this question, this one worked for me! – moth Sep 26 '17 at 20:50
  • I too don't understand why this isn't the best answer. It matches perfectly what the OP question asked for. – Richard Oct 01 '18 at 21:10
  • after some years it's time to answer that. it's easier and shorter to do it that way, also the condition is positive. It's good practice to formulate your conditions in a positive way and not in a negative. Note that a REGULAR EXPRESSION does not return anything. So the demand that a RE should return `false` is not fulfillable if you nitpick.. – Jan. Mar 27 '21 at 16:37
  • Thank you! I agree that the top answer is doing the opposite of what is asked. – Aaron Aug 02 '22 at 15:41
60

/^$|\s+/ if this matched, there's whitespace or its empty.

Jan.
  • 1,925
  • 15
  • 17
  • 1
    Correct. the + isn't needed. Just do not put a * there. ;) – Jan. Nov 01 '11 at 13:38
  • 1
    In some languages (e.g. Ruby), `^` and `$` mean begining or end of the line, not string. But interesting fact is that even under this circumstances the expression will work correctly. – skalee Nov 30 '12 at 01:42
  • 1
    @skalee There's a PCRE Modifier "m" which toggles multiline support – Jan. Dec 04 '12 at 14:06
  • @Jan Yes, but this regexp will work correctly even without this modifier because /^$/ will either match empty string or empty line in multiline string. And empty line => there's line break somewhere => whitespace. – skalee Dec 04 '12 at 15:52
  • In my case `/^$|\s+/ =~ " "` return 0 why ? – Giorgio Desideri Feb 05 '16 at 06:16
  • @GiorgioDesideri You should ask it on a new question, looks interesting why. – edmundo096 Mar 08 '16 at 13:16
  • 4
    how would you invert this? I want it match if the string is not whitespace or empty – Dan Hastings Feb 10 '17 at 16:47
  • And How to avoid not empty string pls help me i have used following regex (?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,15} – Coder Jun 15 '18 at 07:47
  • Please note that in PHP/PCRE this will need the `u` modifier to properly recognize [unicode whitespace](https://en.wikipedia.org/wiki/Whitespace_character#Unicode). – Ro Achterberg Aug 19 '19 at 12:40
25

Most regular expression engines support "counter part" escape sequences. That is, for \s (white-space) there's its counter part \S (non-white-space).

Using this, you can check, if there is at least one non-white-space character with ^\S+$.

PCRE for PHP has several of these escape sequences.

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
  • 1
    The regex is good, but the explanation is a bit misleading. Your regex matches a string that consists entirely of non-whitespace characters and is at least one character in length. Thus, it does more than just "check if there is at least one non-whitespace character". – Tim Pietzcker Nov 01 '11 at 13:39
  • 1
    Why the downvote? The regex is solving the OP's problem - if it matches, the string is not empty and doesn't contain whitespace. – Tim Pietzcker Nov 01 '11 at 13:49
  • 2
    This was the one for me. I simply did `str.match(/\S+/)` – Richard Ayotte May 21 '14 at 13:39
  • I tested this regex for the OP's request. It allows empty strings. They want to also exclude empty strings – Richard Oct 01 '18 at 21:06
10

/^$|\s+/ This matches when empty or white spaces

/(?!^$)([^\s])/ This matches when its not empty or white spaces

Kam Hoe
  • 101
  • 1
  • 3
6

I ended up using something similar to the accepted answer, with minor modifications

(^$)|(\s+$)

Explanation by the Expresso

Select from 2 alternatives (^$)
    [1] A numbered captured group ^$
        Beginning of line ^
        End of line $
    [2] A numbered captured group (\s+$)
        Whitespace, one or more repetitions \s+
        End of line $
oleksii
  • 35,458
  • 16
  • 93
  • 163
6

A little late, but here's a regex I found that returns 0 matches for empty or white spaces:

/^(?!\s*$).+/

You can test this out at regex101

TrieuNomad
  • 1,651
  • 1
  • 21
  • 24
-1

/^[\s]*$/ matches empty strings and strings containing whitespaces only

micfra
  • 2,780
  • 1
  • 23
  • 34