猿问

是否有一个正则表达式来检测一个有效的正则表达式?

是否有一个正则表达式来检测一个有效的正则表达式?

是否可以用另一个正则表达式检测有效的正则表达式?如果是的话,请给出下面的示例代码。



慕容3067478
浏览 703回答 3
3回答

墨色风雨

/^&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# start of string(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# first group start&nbsp; (?:&nbsp; &nbsp; (?:[^?+*{}()[\]\\|]+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # literals and ^, $&nbsp; &nbsp; &nbsp;| \\.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # escaped characters&nbsp; &nbsp; &nbsp;| \[ (?: \^?\\. | \^[^\\] | [^\\^] )&nbsp; &nbsp; &nbsp;# character classes&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (?: [^\]\\]+ | \\. )* \]&nbsp; &nbsp; &nbsp;| \( (?:\?[:=!]|\?<[=!]|\?>)? (?1)?? \)&nbsp; # parenthesis, with recursive content&nbsp; &nbsp; &nbsp;| \(\? (?:R|[+-]?\d+) \)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# recursive matching&nbsp; &nbsp; &nbsp;)&nbsp; &nbsp; (?: (?:[?+*]|\{\d+(?:,\d*)?\}) [?+]? )?&nbsp; &nbsp;# quantifiers&nbsp; | \|&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # alternative&nbsp; )*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # repeat content)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# end first group$&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# end of string/这是一个递归正则表达式,许多regex引擎不支持它。基于PCRE的机构应该支持它。没有空格和注释:/^((?:(?:[^?+*{}()[\]\\|]+|\\.|\[(?:\^?\\.|\^[^\\]|[^\\^])(?:[^\]\\]+|\\.)*\]|\((?:\?[:=!]|\?<[=!]|\?>)?(?1)??\)|\(\?(?:R|[+-]?\d+)\))(?:(?:[?+*]|\{\d+(?:,\d*)?\})[?+]?)?|\|)*)$/NET不直接支持递归。()(?1)和(?R))递归必须转换为计算平衡组:^&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# start of string(?:&nbsp; (?: [^?+*{}()[\]\\|]+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# literals and ^, $&nbsp; &nbsp;| \\.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # escaped characters&nbsp; &nbsp;| \[ (?: \^?\\. | \^[^\\] | [^\\^] )&nbsp; &nbsp;# character classes&nbsp; &nbsp; &nbsp; &nbsp; (?: [^\]\\]+ | \\. )* \]&nbsp; &nbsp;| \( (?:\?[:=!]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| \?<[=!]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| \?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| \?<[^\W\d]\w*>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| \?'[^\W\d]\w*'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# opening of group&nbsp; &nbsp; &nbsp;(?<N>)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#&nbsp; &nbsp;increment counter&nbsp; &nbsp;| \)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# closing of group&nbsp; &nbsp; &nbsp;(?<-N>)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #&nbsp; &nbsp;decrement counter&nbsp; &nbsp;)&nbsp; (?: (?:[?+*]|\{\d+(?:,\d*)?\}) [?+]? )? # quantifiers| \|&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # alternative)*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # repeat content$&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# end of string(?(N)(?!))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # fail if counter is non-zero.压实:^(?:(?:[^?+*{}()[\]\\|]+|\\.|\[(?:\^?\\.|\^[^\\]|[^\\^])(?:[^\]\\]+|\\.)*\]|\((?:\?[:=!]|\?<[=!]|\?>|\?<[^\W\d]\w*>|\?'[^\W\d]\w*')?(?<N>)|\)(?<-N>))(?:(?:[?+*]|\{\d+(?:,\d*)?\})[?+]?)?|\|)*$(?(N)(?!))分享

FFIVE

不太可能。在一个try..catch或者你的语言所提供的一切。

慕姐8265434

如果严格地说正则表达式,而不包括一些实际上是上下文无关语法的正则表达式实现,则不会。正则表达式有一个限制,使得不可能编写匹配所有和唯一正则表达式的正则表达式。您无法匹配实现,如大括号的配对。Regexes使用了许多这样的结构,让我们以[]为例。无论何时有[必须有匹配]。简单到一个正则表达式“[.*]”。REXEX之所以不可能,是因为它们可以嵌套。如何编写与嵌套括号匹配的正则表达式?答案是,没有无限长的正则表达式是不可能的。您可以通过蛮力匹配任意数量的嵌套父母,但您永远无法匹配任意长的嵌套括号集。这种功能通常被称为计数(您正在计算嵌套的深度)。根据定义,正则表达式不具备计数能力。编辑:最后写了一篇关于此的博客文章:正则表达式限制
随时随地看视频慕课网APP
我要回答