本文介绍了是否有正则表达式来检测有效的正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to detect a valid regular expression with another regular expression? If so please give example code below.

推荐答案

/
^                                             # start of string
(                                             # first group start
  (?:
    (?:[^?+*{}()[\]\\|]+                      # literals and ^, $
     | \\.                                    # escaped characters
     | \[ (?: \^?\\. | \^[^\\] | [^\\^] )     # character classes
          (?: [^\]\\]+ | \\. )* \]
     | \( (?:\?[:=!]|\?<[=!]|\?>)? (?1)?? \)  # parenthesis, with recursive content
     | \(\? (?:R|[+-]?\d+) \)                 # recursive matching
     )
    (?: (?:[?+*]|\{\d+(?:,\d*)?\}) [?+]? )?   # quantifiers
  | \|                                        # alternative
  )*                                          # repeat content
)                                             # end first group
$                                             # end of string
/

这是一个递归正则表达式,许多正则表达式引擎不支持.基于 PCRE 的应该支持它.

This is a recursive regex, and is not supported by many regex engines. PCRE based ones should support it.

没有空格和注释:

/^((?:(?:[^?+*{}()[\]\\|]+|\\.|\[(?:\^?\\.|\^[^\\]|[^\\^])(?:[^\]\\]+|\\.)*\]|\((?:\?[:=!]|\?<[=!]|\?>)?(?1)??\)|\(\?(?:R|[+-]?\d+)\))(?:(?:[?+*]|\{\d+(?:,\d*)?\})[?+]?)?|\|)*)$/

.NET 不直接支持递归.((?1)(?R) 结构.)必须将递归转换为计算平衡组:


.NET does not support recursion directly. (The (?1) and (?R) constructs.) The recursion would have to be converted to counting balanced groups:

^                                         # start of string
(?:
  (?: [^?+*{}()[\]\\|]+                   # literals and ^, $
   | \\.                                  # escaped characters
   | \[ (?: \^?\\. | \^[^\\] | [^\\^] )   # character classes
        (?: [^\]\\]+ | \\. )* \]
   | \( (?:\?[:=!]
         | \?<[=!]
         | \?>
         | \?<[^\W\d]\w*>
         | \?'[^\W\d]\w*'
         )?                               # opening of group
     (?<N>)                               #   increment counter
   | \)                                   # closing of group
     (?<-N>)                              #   decrement counter
   )
  (?: (?:[?+*]|\{\d+(?:,\d*)?\}) [?+]? )? # quantifiers
| \|                                      # alternative
)*                                        # repeat content
$                                         # end of string
(?(N)(?!))                                # fail if counter is non-zero.

紧凑:

^(?:(?:[^?+*{}()[\]\\|]+|\\.|\[(?:\^?\\.|\^[^\\]|[^\\^])(?:[^\]\\]+|\\.)*\]|\((?:\?[:=!]|\?<[=!]|\?>|\?<[^\W\d]\w*>|\?'[^\W\d]\w*')?(?<N>)|\)(?<-N>))(?:(?:[?+*]|\{\d+(?:,\d*)?\})[?+]?)?|\|)*$(?(N)(?!))

来自评论:

这会验证替换和翻译吗?

它将仅验证替换和翻译的正则表达式部分.s//.../

It will validate just the regex part of substitutions and translations. s/<this part>/.../

理论上不可能用一个正则表达式来匹配所有有效的正则表达式语法.

如果正则表达式引擎支持递归是有可能的,比如 PCRE,但那真的不能再称为正则表达式了.

It is possible if the regex engine supports recursion, such as PCRE, but that can't really be called regular expressions any more.

确实,递归正则表达式"不是正则表达式.但这是一个经常被接受的对正则表达式引擎的扩展......具有讽刺意味的是,这个扩展的正则表达式与扩展的正则表达式不匹配.

理论上,理论和实践是一样的.实际上,它们不是."几乎所有了解正则表达式的人都知道正则表达式不支持递归.但是 PCRE 和大多数其他实现支持的不仅仅是基本的正则表达式.

"In theory, theory and practice are the same. In practice, they're not." Almost everyone who knows regular expressions knows that regular expressions does not support recursion. But PCRE and most other implementations support much more than basic regular expressions.

在 grep 命令中将它与 shell 脚本一起使用,它向我显示了一些错误.. grep: {} 的内容无效.我正在制作一个脚本,可以使用 grep 代码库来查找所有包含正则表达式的文件

这个模式利用了一个叫做递归正则表达式的扩展.正则表达式的 POSIX 风格不支持这一点.您可以尝试使用 -P 开关,以启用 PCRE 正则表达式风格.

This pattern exploits an extension called recursive regular expressions. This is not supported by the POSIX flavor of regex. You could try with the -P switch, to enable the PCRE regex flavor.

正则表达式本身不是正则语言,因此不能被正则表达式解析..."

这适用于经典的正则表达式.一些现代实现允许递归,这使其成为一种上下文无关语言,尽管对于此任务来说有些冗长.

This is true for classical regular expressions. Some modern implementations allow recursion, which makes it into a Context Free language, although it is somewhat verbose for this task.

我知道你在哪里匹配 []()/\.和其他特殊的正则表达式字符.你在哪里允许非特殊字符?看起来这将匹配 ^(?:[\.]+)$,而不是 ^abcdefg$.这是一个有效的正则表达式.

[^?+*{}()[\]\\|] 将匹配任何单个字符,而不是任何其他结构的一部分.这包括文字(a - z)和某些特殊字符(^$.).

[^?+*{}()[\]\\|] will match any single character, not part of any of the other constructs. This includes both literal (a - z), and certain special characters (^, $, .).

这篇关于是否有正则表达式来检测有效的正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-21 08:46