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+$
\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'.