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.
^\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*$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With