![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 135
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 135
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,435
Rep Power: 8
![]() |
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 |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 135
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,435
Rep Power: 8
![]() |
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. |
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 647
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#7 |
|
Not a user?
Join Date: Sep 2007
Posts: 189
Rep Power: 1
![]() |
Re: Regex, or I'm thick
doesn't string have an isemptyornull method in C#?
|
|
|
|
|
|
#8 |
|
Expert Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 706
Rep Power: 3
![]() |
Re: Regex, or I'm thick
Jabo - yes, but " " would return false from IsEmptyOrNull.
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#9 | |
|
Not a user?
Join Date: Sep 2007
Posts: 189
Rep Power: 1
![]() |
Re: Regex, or I'm thick
Quote:
Edit: no, maybe not, I see what you're saying. Last edited by Jabo; Mar 28th, 2008 at 12:05 AM. Reason: blonde moment |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Regex help | titaniumdecoy | Other Programming Languages | 1 | Nov 20th, 2007 6:41 PM |
| RegEx problem.. | cyphix | PHP | 4 | Mar 4th, 2007 6:24 AM |
| Email validating regex | Jimbo | PHP | 6 | Jul 6th, 2006 2:58 PM |
| regex (preg_replace) parsing | para | PHP | 2 | Dec 31st, 2005 6:30 PM |
| Regex help | Simon Gray | PHP | 10 | Sep 7th, 2005 11:02 AM |