tags:

views:

286

answers:

6

Hi,

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.

Thanks.

Christian

+19  A: 
^(?!\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 positive 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.

Tim Pietzcker
*only whitespace* or an empty string, +1
tanascius
As well as the empty string. (A small distinction, but sometimes an important one, though not in this case as csetzkorn wants that.)
JAB
@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
thanks but doesn't seem to work ...
csetzkorn
@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
csetzkorn
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!
csetzkorn
The negation would be `\S` which would match any non-whitespace character
gnarf
Thanks ^(?!\s*$).+ is the best for my particular situation.
csetzkorn
A: 

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.

Kevin van Zanten
But you will not match a "tab" character, which is still whitespace. A \s instead of the [ ] fixes that.
Hans Kesting
This is true, thank you for pointing it out.
Kevin van Zanten
+22  A: 

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

SLaks
Certainly the easiest solution ^^
tanascius
Well that's useful.
JAB
If you're not on .Net 4.0, you can use String.IsNullOrEmpty(variable.Trim()) to achieve essentially the same thing.
Ian P
very good @ slacks
4thpage
I have to use regular expressions in my chosen validation framework. thanks anyway.
csetzkorn
@Ian: Unless it's `null`.
SLaks
@IanP - no you can't. It'll fail if variable is null.
Rob Levine
+2  A: 

You could also use:

public static bool IsWhiteSpace(string s) 
{
    return s.Trim().Length == 0;
}
jjnguy
I have to use regular expressions in my chosen validation framework. thanks anyway.
csetzkorn
It will return true with any text (which doesn't contain trailing or leading whitspace). `IsWhiteSpace("test")` => true.
Shimrod
@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
@Shimrod, yeah, my bad. I was thinking one thing, but wrote another. It has been fixed.
jjnguy
+2  A: 

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

polygenelubricants
A: 

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

recursive