Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Regex, or I'm thick (http://www.programmingforums.org/showthread.php?t=15497)

Arla Mar 27th, 2008 1:06 PM

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?

Arla Mar 27th, 2008 1:30 PM

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.

Ooble Mar 27th, 2008 3:28 PM

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

Arla Mar 27th, 2008 3:38 PM

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.

Ooble Mar 27th, 2008 3:48 PM

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.

OpenLoop Mar 27th, 2008 5:41 PM

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".

[^a-z] matches any single character that is not a lowercase letter from "a" to "z". As above, literal characters and ranges can be mixed.

^[hc]at matches "hat" and "cat", but only at the beginning of the string or line.


http://en.wikipedia.org/wiki/Regular_expression

Jabo Mar 27th, 2008 11:15 PM

Re: Regex, or I'm thick
 
doesn't string have an isemptyornull method in C#?

Jimbo Mar 28th, 2008 12:02 AM

Re: Regex, or I'm thick
 
Jabo - yes, but " " would return false from IsEmptyOrNull.

Jabo Mar 28th, 2008 12:04 AM

Re: Regex, or I'm thick
 
Quote:

Originally Posted by Arla (Post 143063)
I want a REGEX that will tell me if a string contains anything other than whitespace.

Seems useful in this case to me.

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