Matching an empty string with regex

前端 未结 5 861
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 20:21

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

相关标签:
5条回答
  • 2020-12-20 20:29

    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.

    0 讨论(0)
  • 2020-12-20 20:36

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

    0 讨论(0)
  • 2020-12-20 20:41

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

    0 讨论(0)
  • 2020-12-20 20:48

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

    0 讨论(0)
  • 2020-12-20 20:53

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

    0 讨论(0)
提交回复
热议问题