gpt4 book ai didi

regex - 是否有正则表达式可以检测有效的正则表达式?

转载 作者:行者123 更新时间:2023-12-03 03:51:51 25 4
gpt4 key购买 nike

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

最佳答案

/
^ # 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 的应该支持它。

没有空格和注释:

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

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

^                                         # 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)(?!))

来自评论:

Will this validate substitutions and translations?

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

It is not theoretically possible to match all valid regex grammars with a regex.

如果正则表达式引擎支持递归,例如PCRE,这是可能的,但那不能再称为正则表达式了。

Indeed, a "recursive regular expression" is not a regular expression. But this an often-accepted extension to regex engines... Ironically, this extended regex doesn't match extended regexes.

“从理论上讲,理论和实践是相同的。但在实践中,它们却不相同。”几乎所有了解正则表达式的人都知道,正则表达式不支持递归。但 PCRE 和大多数其他实现支持的不仅仅是基本正则表达式。

using this with shell script in the grep command , it shows me some error.. grep: Invalid content of {} . I am making a script that could grep a code base to find all the files that contain regular expressions

此模式利用了称为递归正则表达式的扩展。 POSIX 风格的正则表达式不支持这一点。您可以尝试使用 -P 开关来启用 PCRE 正则表达式风格。

Regex itself "is not a regular language and hence cannot be parsed by regular expression..."

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

I see where you're matching []()/\. and other special regex characters. Where are you allowing non-special characters? It seems like this will match ^(?:[\.]+)$, but not ^abcdefg$. That's a valid regex.

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

关于regex - 是否有正则表达式可以检测有效的正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/172303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com