Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 27th, 2008, 2:06 PM   #1
Arla
Professional Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 346
Rep Power: 4 Arla is on a distinguished road
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 is offline   Reply With Quote
Old Mar 27th, 2008, 2:30 PM   #2
Arla
Professional Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 346
Rep Power: 4 Arla is on a distinguished road
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.
Arla is offline   Reply With Quote
Old Mar 27th, 2008, 4:28 PM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 27th, 2008, 4:38 PM   #4
Arla
Professional Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 346
Rep Power: 4 Arla is on a distinguished road
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.
Arla is offline   Reply With Quote
Old Mar 27th, 2008, 4:48 PM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 27th, 2008, 6:41 PM   #6
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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
OpenLoop is offline   Reply With Quote
Old Mar 28th, 2008, 12:15 AM   #7
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 308
Rep Power: 2 Jabo is on a distinguished road
Re: Regex, or I'm thick

doesn't string have an isemptyornull method in C#?
Jabo is offline   Reply With Quote
Old Mar 28th, 2008, 1:02 AM   #8
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
Re: Regex, or I'm thick

Jabo - yes, but " " would return false from IsEmptyOrNull.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Mar 28th, 2008, 1:04 AM   #9
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 308
Rep Power: 2 Jabo is on a distinguished road
Re: Regex, or I'm thick

Quote:
Originally Posted by Arla View Post
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.

Last edited by Jabo; Mar 28th, 2008 at 1:05 AM. Reason: blonde moment
Jabo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex help titaniumdecoy Other Programming Languages 1 Nov 20th, 2007 7:41 PM
RegEx problem.. cyphix PHP 4 Mar 4th, 2007 7:24 AM
Email validating regex Jimbo PHP 6 Jul 6th, 2006 3:58 PM
regex (preg_replace) parsing para PHP 2 Dec 31st, 2005 7:30 PM
Regex help Simon Gray PHP 10 Sep 7th, 2005 12:02 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:11 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC