What is a regex to match ONLY an empty string?

后端 未结 10 1648
忘掉有多难
忘掉有多难 2020-12-01 13:24

There are lots of posts about regexs to match a potentially empty string, but I couldn\'t readily find any which provided a regex which only matched an emp

10条回答
  •  遥遥无期
    2020-12-01 14:10

    I believe Python is the only widely used language that doesn't support \z in this way (yet). There are Python bindings for Russ Cox / Google's super fast re2 C++ library that can be "dropped in" as a replacement for the bundled re.

    There's an excellent discussion (with workarounds) for this at Perl Compatible Regular Expression (PCRE) in Python, here on SO.

    python
    Python 2.7.11 (default, Jan 16 2016, 01:14:05) 
    [GCC 4.2.1 Compatible FreeBSD Clang 3.4.1 on freebsd10
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import re2 as re
    >>> 
    >>> re.match(r'\A\z', "")
    
    

    @tchrist's answer is worth the read.

提交回复
热议问题