是否有可能有匹配所有有效正则表达式的正则表达式?

发布于 2024-09-03 02:41:27 字数 218 浏览 2 评论 0原文

是否可以仅使用正则表达式来检测给定字符串是否是有效的正则表达式?

假设我有一些字符串,它们可能是也可能不是有效的正则表达式。我想要一个正则表达式与对应于有效正则表达式的那些字符串相匹配。这可能吗?或者我是否使用一些更高级别的语法(即上下文无关语言)来检测这一点?如果我使用某些扩展版本的正则表达式(例如 Perl 正则表达式),这会影响吗?

如果可能的话,匹配正则表达式的正则表达式是什么?

Is it possible to detect if a given string is valid regular expression, using just regular expressions?

Say I have some strings, that may or may not be a valid regular expressions. I'd like to have a regular expression matches those string that correspond to valid regular expression. Is that possible? Or do I have use some higher level grammar (i.e. context free language) to detect this? Does it affect if I am using some extended version of regexps like Perl regexps?

If that is possible, what the regexp matching regexp is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舞袖。长 2024-09-10 02:41:27

不,这是不可能的。这是因为有效的正则表达式涉及分组,这需要平衡括号。

平衡分隔符不能与正则表达式匹配;它们必须与上下文无关语法匹配。 (该文章的第一个示例涉及平衡括号。)

No, it is not possible. This is because valid regular expressions involve grouping, which requires balanced parentheses.

Balanced delimiters cannot be matched by a regular expression; they must instead be matched with a context-free grammar. (The first example on that article deals with balanced parentheses.)

仙气飘飘 2024-09-10 02:41:27

请参阅此处的精彩文章:

正则表达式的正则表达式?

答案是正则表达式不是使用常规语法编写的,而是使用上下文无关的语法编写的。

See an excellent write-up here:

Regular expression for regular expressions?

The answer is that regexes are NOT written using a regular grammar, but a context-free one.

月牙弯弯 2024-09-10 02:41:27

如果您的问题是“匹配所有有效的正则表达式”,答案是(也许令人惊讶)“是”。正则表达式 .* 匹配所有有效(和无效)正则表达式,但对于确定您是否正在查看有效正则表达式几乎没有用处。

然而,由于问题是“匹配所有且仅有效的正则表达式”,答案是(正如 DVK 和 Platinum Azure 所说的“不”)。

If your question had been "match all valid regular expressions", the answer is (perhaps surprisingly) 'yes'. The regular expression .* match all valid (and non-valid) regular expressions, but is pretty useless for determining if you're looking at a valid one.

However, as the question is "match all and only valid regular expressions", the answer is (as DVK and Platinum Azure" have said 'no'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文