79

Is it possible to use a regular expression to detect anything that is NOT an "empty string" like this:

string s1 = "";
string s2 = " ";
string s3 = "  ";
string s4 = "   ";

etc.

I know I could use trim etc. but I would like to use a regular expression.

Dale K
  • 25,246
  • 15
  • 42
  • 71
cs0815
  • 16,751
  • 45
  • 136
  • 299
  • I am sorry I edited my question as it had to be 'negated' – cs0815 Jun 21 '10 at 14:56
  • 4
    If I may, what's the compelling reason to use a regular expression rather than the built-in function? – Jim Dagg Jun 21 '10 at 17:04
  • 1
    In .net vernacular, only your first example ("") is considered an "empty string". The others are purely whitespace--but not empty. This seemingly minor difference has yielded some overly complicated answers below. – Richard II Apr 30 '15 at 18:06

9 Answers9

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

will match any string that contains at least one non-space character.

So

if (Regex.IsMatch(subjectString, @"^(?!\s*$).+")) {
    // Successful match
} else {
    // Match attempt failed
}

should do this for you.

^ anchors the search at the start of the string.

(?!\s*$), a so-called negative lookahead, asserts that it's impossible to match only whitespace characters until the end of the string.

.+ will then actually do the match. It will match anything (except newline) up to the end of the string. If you want to allow newlines, you'll have to set the RegexOptions.Singleline option.


Left over from the previous version of your question:

^\s*$

matches strings that contain only whitespace (or are empty).

The exact opposite:

^\S+$

matches only strings that consist of only non-whitespace characters, one character minimum.

jacekbe
  • 499
  • 1
  • 5
  • 18
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • 2
    *only whitespace* or an empty string, +1 – tanascius Jun 21 '10 at 14:34
  • 2
    As well as the empty string. (A small distinction, but sometimes an important one, though not in this case as csetzkorn wants that.) – JAB Jun 21 '10 at 14:36
  • @tanascius, @JAB: Thanks for noticing this. I have updated my answer, obviously fast enough so it's not showing up as an edit :) – Tim Pietzcker Jun 21 '10 at 14:37
  • @csetzkorn: “doesn’t seem to work” is a decidedly unhelpful remark. The code *definitely* works so you need to provide way more info to let us help you. – Konrad Rudolph Jun 21 '10 at 14:41
  • I intend to use this: http://fluentvalidation.codeplex.com/wikipage?title=Validators&referringTitle=Documentation&ANCHOR#Regex The provided regex does not work in this case. The framework has an Empty validator which work with truly empty strings but not 'empty strings' which contain a space. that's why I want to use a regex. – cs0815 Jun 21 '10 at 14:46
  • 1
    I think I know why it does not work. How do I negate your suggestion? Meaning - match everything but not empty strings. Thanks and sorry about the confusion! – cs0815 Jun 21 '10 at 14:53
  • 4
    The negation would be `\S` which would match any non-whitespace character – gnarf Jun 21 '10 at 14:58
  • Thanks ^(?!\s*$).+ is the best for my particular situation. – cs0815 Jun 21 '10 at 15:09
  • 2
    (?!\s*$) is a negative lookahead, not positive – jacekbe Oct 11 '18 at 17:43
  • @jacekbe: Of course, thanks. Wow, this has slipped by so many people for so many years... – Tim Pietzcker Oct 12 '18 at 09:43
31

In .Net 4.0, you can also call String.IsNullOrWhitespace.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
8

Assertions are not necessary for this. \S should work by itself as it matches any non-whitespace.

recursive
  • 83,943
  • 34
  • 151
  • 241
  • 2
    This is the right answer! Many of the others are overly complicated, because a.) they myopically focus on the OP's term "empty string" when the examples given clearly include strings consisting of various amounts of whitespace, or b.) they missed the clearly-stated requirement that the OP wants a regex solution. – Richard II Apr 30 '15 at 18:11
7

What about?

/.*\S.*/

This means

/ = delimiter
.* = zero or more of anything but newline
\S = anything except a whitespace (newline, tab, space)

so you get
match anything but newline + something not whitespace + anything but newline

Timo Huovinen
  • 53,325
  • 33
  • 152
  • 143
3

You can do one of two things:

  • match against ^\s*$; a match means the string is "empty"
    • ^, $ are the beginning and end of string anchors respectively
    • \s is a whitespace character
    • * is zero-or-more repetition of
  • find a \S; an occurrence means the string is NOT "empty"
    • \S is the negated version of \s (note the case difference)
    • \S therefore matches any non-whitespace character

References

Related questions

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
2

You could also use:

public static bool IsWhiteSpace(string s) 
{
    return s.Trim().Length == 0;
}
jjnguy
  • 136,852
  • 53
  • 295
  • 323
  • I have to use regular expressions in my chosen validation framework. thanks anyway. – cs0815 Jun 21 '10 at 14:42
  • 1
    It will return true with any text (which doesn't contain trailing or leading whitspace). `IsWhiteSpace("test")` => true. – Shimrod Jun 21 '10 at 14:44
  • @csetz I understand that. However, other people may find value in knowing there are other ways to solve this problem. Some people don't like regex at all. – jjnguy Jun 21 '10 at 14:44
  • 1
    @Shimrod, yeah, my bad. I was thinking one thing, but wrote another. It has been fixed. – jjnguy Jun 21 '10 at 14:45
1

We can also use space in a char class, in an expression similar to one of these:

(?!^[ ]*$)^\S+$
(?!^[ ]*$)^\S{1,}$
(?!^[ ]{0,}$)^\S{1,}$
(?!^[ ]{0,1}$)^\S{1,}$

depending on the language/flavor that we might use.

RegEx Demo

Test

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"(?!^[ ]*$)^\S+$";
        string input = @"

            abcd
            ABCD1234
            #$%^&*()_+={}
            abc def
            ABC 123
            ";
        RegexOptions options = RegexOptions.Multiline;

        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}

C# Demo


If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Emma
  • 27,428
  • 11
  • 44
  • 69
0

I think [ ]{4} might work in the example where you need to detect 4 spaces. Same with the rest: [ ]{1}, [ ]{2} and [ ]{3}. If you want to detect an empty string in general, ^[ ]*$ will do.

Fusyion
  • 704
  • 1
  • 12
  • 23
-1

Create "regular expression to detect empty string", and then inverse it. Invesion of regular language is the regular language. I think regular expression library in what you leverage - should support it, but if not you always can write your own library.

grep --invert-match

Konstantin Burlachenko
  • 5,233
  • 2
  • 41
  • 40