php类或正则表达式检查正则表达式模式本身(至少检查分隔符/语法..)

我写了一个带有相当数量的正则表达式的valitor类。

使用Eclipse Regex Util,它 能够报告正则表达式模式“错误”,所以我想知道我是否能找到一个正则表达式 - 或一个php子类 - 来检查我正在调用的正则表达式模式是一个正确的正则表达式模式,嵌套一个专用的类 方法,在检查/测试它们之上。 </ p>
</ div>

展开原文

原文

I wrote a valitor class with a fair amount of regex in there.
Using Eclipse Regex Util, it is able to report regex pattern "mistakes", so I'm wondering whether I could find a regex - or a php subclass - , to check that the regex pattern I'm calling is a correct regex pattern, nesting a dedicated in-class method, on top of checking/testing them unitary.

2个回答



最简单的方法是尝试在空字符串上使用 preg_match()</ code>和正则表达式; 如果它返回 False </ code>,则发生错误,意味着该模式无效。</ p>
</ div>

展开原文

原文

The easiest way would be to just try using preg_match() with the regex on an empty string; if it returns False, an error has occurred, meaning that the pattern is invalid.

dqhmtpuy94946
dqhmtpuy94946 thx,这与我正在寻找的简单聪明的想法相匹配,即使我在梦想PHP中的一些现有工具。
9 年多之前 回复



正如Tim正确建议的那样,验证</ em> PCRE语法正则表达式的最佳方法是尝试编译它( 通过 preg_match()</ code>运行它。 应该选择他作为最佳答案(我已经给它+1了)。</ p>

但是,如果有人有兴趣了解如何分解PCRE语法正则表达式来检查 正确匹配(可能是嵌套的)括号组,我编写了一个JavaScript实用程序,它正是这样做的。 该脚本的用途也非常有用:它允许网页显示正则表达式,以便用户可以将鼠标悬停在任何支架上以突出显示并查看其匹配的括号。 它使用工具提示识别编号的捕获组,并以红色突出显示任何错误的孤立不匹配括号。</ p>

动态(?:正则表达式突出显示)++ with Javascript!</ h2>

< p>这是描述脚本使用的正则表达式的文档页面:

DynamicRegexHighlighter </ p>

这里是测试页面,您可以在其中对任何正则表达式应用动态突出显示:

DynamicRegexHighlighterTester </ p>

如果你是正则表达式,我认为你会同意这是一个漂亮的 很酷的工具。 测试页面非常便于读取未以注释,自由间隔模式编写的长而复杂的正则表达式。</ p>

这是在MIT许可下发布的,项目在GitHub上托管 。</ p>
</ div>

展开原文

原文

As Tim correctly suggests, the best way to validate a PCRE syntax regex is to try and compile it (by running it through preg_match()). His should be selected as the best answer (I've already given it my +1).

However, if anyone is interested in seeing how one can decompose a PCRE syntax regex to check for proper matching of (possibly nested) parentheses groups, I've written a JavaScript utility which does precisely this. The purpose of the script is pretty useful, too: It allows a web page to display a regex such that a user can mouse over any bracket to highlight and see its matching bracket. It identifies numbered capturing groups with a tooltip and highlights any erroneous orphaned unmatched parentheses in red.

Dynamic (?:Regex Highlighting)++ with Javascript!

Here is the documentation page which describes the regular expressions used by the script:
DynamicRegexHighlighter

And here is the tester page where you can apply dynamic highlighting to any regex:
DynamicRegexHighlighterTester

If you are into regex, I think that you will agree that this is a pretty cool tool. The tester page is very handy for reading long, complex regexes that are not written in commented, free-spacing mode.

This is released under the MIT license and the project is hosted over on GitHub.

dpwfh874876
dpwfh874876 谢谢,祝贺,这是一个令人印象深刻的工具!! 将引擎转换为PHP对我来说非常好,因为目的是在服务器端调试/控制/管理无数的php正则表达式,“即时”(哼声......服务器端)。
9 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐