简体   繁体   English

正则表达式'(?!^()$)'可以匹配吗?

[英]Can a regex '(?!^()$)' match anything?

Problem 问题

In a very special case, my negative lookahead is an empty list: 在一个非常特殊的情况下,我的否定前瞻是一个空列表:

(?!^()$)

Is there any string that matches it? 是否有任何匹配的字符串?

Clarification 澄清度

Let's say: 比方说:

(?!^()$)^(.*)$

Will it match everything? 它会匹配所有东西吗?

Literally anything, beside empty string. 从字面上看,空字符串旁边的任何东西。

The regex contains 2 parts, (?!^()$) and ^(.*)$ : 正则表达式包含(?!^()$)^(.*)$

  1. (?!^()$) Is a negative zero-width match for empty string. (?!^()$)空字符串的零宽度负匹配。 In order words, string.Empty is out. 按照顺序, string.Empty不在列。

  2. ^(.*)$ is a full match for anything except newlines 1 repeated 0 to many times, so basically anything. ^(.*)$完全匹配除换行1重复0到很多次以外的所有内容,因此基本上是任何内容。

Note : 1. exception new line character 注意:1.例外换行符

(?!^()$) can be simplified to (?!^$) since () is a null group and will match at any position, all the time. (?!^()$)可以简化为(?!^$)因为()是一个空组,并且将始终在任何位置匹配。

So now you're saying "match at any and every position where the start and end anchors aren't right next to one another, or in other words, we aren't at an empty string". 因此,现在您说的是“在开始和结束锚点彼此不相邻的任何位置进行匹配,换句话说,我们不是在空字符串上”。

Therefore (?!^$) can match at every position in a string that isn't just empty or a newline . 因此(?!^$) 可以在不仅为空或换行的字符串中的每个位置都匹配

(?!^()$)^(.*)$ is "match everywhere but at empty string" plus ^.*$ which will "match at and consume every single line, empty or not" (anchors ^ and $ have no effect in this case). (?!^()$)^(.*)$是“到处都匹配,但在空字符串处”加上^.*$将“匹配并消耗每行,无论是否为空”(锚点^$没有在这种情况下效果)。 So it's essentially saying "consume (at least) one or more characters in a string", which can be distilled down to simply .+ 因此,这实际上是在说“在一个字符串中至少消耗一个或多个字符”,可以将其简化为.+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM