匹配有效正则表达式的正则表达式 [英] Regexp that matches valid regexps
问题描述
是否有匹配有效正则表达式的正则表达式?
Is there a regular expression that matches valid regular expressions?
(我知道正则表达式有多种风格.一种可以.)
(I know there are several flavors of regexps. One would do.)
推荐答案
如果您只想检查正则表达式是否有效,只需尝试使用您正在使用的任何编程语言或正则表达式库对其进行编译.
If you merely want to check whether a regular expression is valid or not, simply try to compile it with whichever programming language or regular expression library you're working with.
解析正则表达式绝非易事.作为 RegexBuddy 的作者,我已经在那个街区遇到了几次.如果您真的想这样做,请使用正则表达式来标记输入,并将解析逻辑留给程序代码.也就是说,您的正则表达式将匹配一个正则表达式标记 (^
, $
, \w
, (
, >)
等),您的程序代码将检查它们的顺序是否正确.
Parsing regular expressions is far from trivial. As the author of RegexBuddy, I have been around that block a few times. If you really want to do it, use a regex to tokenize the input, and leave the parsing logic to procedural code. That is, your regex would match one regex token (^
, $
, \w
, (
, )
, etc.) at a time, and your procedural code would check if they're in the right order.
这篇关于匹配有效正则表达式的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!