![]() |
Regex, or I'm thick
Can't for the LIFE of me figure how to do this, and it seems like it would be stupidly simple.
I want a REGEX that will tell me if a string contains anything other than whitespace. I've tried @"\s" but that just looks for a single whitespace (so a single whitespace anywhere in the text), @"\s*" but that seems to match anything (and everything?) Where am I going wrong? I tried @"\s*^\S" but that again seems to match anything, what the hell am I doing wrong? |
Re: Regex, or I'm thick
Well this seemed to do it
@"^\s" But I'm utterly baffled as to why, this comes up true for a string that's just whitespace, and false for a string with characters, which seems to be utterly backwards to me. |
Re: Regex, or I'm thick
What that expression will do is look for one character of whitespace at the start of a string. Try a string with a space in front and it should match.
Try this: :
@"^\s+$" ^ = beginning of string\s = whitespace + = there should be one or more of the previous entity $ = end of string |
Re: Regex, or I'm thick
Ooble, thanks, for some reason I had the ^ as meaning "not", somewhere I'm sure one of the many pages I read said that. Anyway, thanks again.
|
Re: Regex, or I'm thick
It can mean "not" in a character class. For example:
^[^xyz\s]+$ would match any string that doesn't contain whitespace or the characters "x", "y" or "z".Regular-Expressions.info is a brilliant resource for regexes. |
Re: Regex, or I'm thick
From wikipedia.org:
:
[^ ] Matches a single character that is not contained within the brackets. For example, [^abc] matches any character other than "a", "b", or "c". http://en.wikipedia.org/wiki/Regular_expression |
Re: Regex, or I'm thick
doesn't string have an isemptyornull method in C#?
|
Re: Regex, or I'm thick
Jabo - yes, but " " would return false from IsEmptyOrNull.
|
Re: Regex, or I'm thick
Quote:
Edit: no, maybe not, I see what you're saying. |
| All times are GMT -5. The time now is 1:25 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC