9

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.

holographic-principle
  • 19,688
  • 10
  • 46
  • 62
genesys
  • 310
  • 1
  • 4
  • 11

5 Answers5

22

^\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+$

enter image description here

^\s*$

enter image description here

arshajii
  • 127,459
  • 24
  • 238
  • 287
4

^\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.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
1

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

JDong
  • 2,304
  • 3
  • 24
  • 42
1

^\s+$ does NOT match an empty string. It matches a string of one or more whitespace symbols (spaces, tabs, linefeeds, etc.)

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
1

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.

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131