What is a regex to match ONLY an empty string?

后端 未结 10 1657
忘掉有多难
忘掉有多难 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:07

    As explained in http://www.regular-expressions.info/anchors.html under the section "Strings Ending with a Line Break", \Z will generally match before the end of the last newline in strings that end in a newline. If you want to only match the end of the string, you need to use \z. The exception to this rule is Python.

    In other words, to exclusively match an empty string, you need to use /\A\z/.

提交回复
热议问题