I've seen posts here on stackoverflow that say that the regex ^$ will match an empty string... So it made me think... why not something like this: ^\s+$ - does that not also work? I know it's more typing, but it in my mind, it also makes more sense. I've not used a whole lot of regex before, but it seems like my need for them is becoming greater as the days go by - so I'm taking the hint and attempting to learn.
- 19,688
- 10
- 46
- 62
- 310
- 1
- 4
- 11
5 Answers
^\s+$- does that not also work?
Not for matching an empty string. In general, X+ means X one or more times. So, \s+ cannot match the empty string - it requires at least one \s in order to match.
^ \s + $
| | | |
start of string ---------------------+ | | |
whitespace character ------------------+ | |
one or more of what precedes -------------+ |
end of string ------------------------------+
Now, X* means X 0 or more times, so ^\s*$ would indeed match an empty string.
^\s+$

^\s*$

- 127,459
- 24
- 238
- 287
^\s+$ will match a sequence of one or more whitespaces, which is not an empty string at all.
An empty string does not contain any character, not even whitespace. However, if you use ^\s*$, it will match an empty string in addition to whitespaces.
- 209,639
- 45
- 409
- 525
-
1"whitespace-only" is actually a quite useful category of strings :-) – John Dvorak Jul 12 '13 at 15:43
\s is the character class for whitespace. ^\s+$ would match both "\t\n" and "\t\t". They look empty, but are not. Spaces, tabs, and newlines are characters too! By using ^$, you match the beginning of the string with ^ immediately followed by the end of the string $. Note that matching the regular expression '' will also match empty strings, but match them anywhere.
Python example:
empty_string_matches = re.findall('', 'hello world')
empty_line_matches = re.findall('^$', 'hello world')
print "Matches for '':", empty_string_matches
print "Matches for '^$':", empty_line_matches
returns
Matches for '': ['', '', '', '', '', '', '', '', '', '', '', '']
Matches for '^$': []
Because there is an empty string between each letter in 'hello world'.
- 2,304
- 3
- 24
- 42
^\s+$ does NOT match an empty string. It matches a string of one or more whitespace symbols (spaces, tabs, linefeeds, etc.)
- 26,085
- 12
- 82
- 103
As others said, you probably mean ^\s*$, not ^\s+$, because ^\s+$ will fail to match the empty string, .
Whether ^\s*$ matches an empty string depends on your definition of "empty". Like ^$, it will match the completely empty string . Unlike ^$, it will also match a string consistening of only whitespace characters like spaces and tabs, such as . Which is the "right" definition of "empty" depends on the situation.
- 29,210
- 11
- 96
- 131