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+$
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.
^\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*$
^\s+$ does NOT match an empty string. It matches a string of one or more whitespace symbols (spaces, tabs, linefeeds, etc.)
\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'.
^\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.