算法查找字符串一个特定的正则表达式匹配 [英] Algorithm for finding strings that a specific Regex will match

查看:125
本文介绍了算法查找字符串一个特定的正则表达式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个正则表达式

,我试图找到一个与其匹配的字符串。类似Django是如何逆转他们,但在C#中。有没有做任何预先制作的C#库






编辑:移动项目来 rel=\"nofollow\">谷歌代码。



电流测试结果



  ^ ABC $> ABC:通
\Aa>答:通
z\Z> Z:通
z\z> Z:通
z\z> Z:通
\G\(a\)GT; \(a\):通
ab\b> AB:通
a\Bb> AB:通
\a> :通
[\b]≥ :通
\t> \t:通
\r> \r:通
\v> ♂:通
\f> \f:通
\\\
> \\\
:通
\e> ←:通
\141>答:通
\x61>答:通
\cC> ♥:通
\\\a>答:通
\\> \\:通
[ABC]>答:通
[^ ABC]> I:通
[A-Z] GT;答:通过
。 >电话号码:通
\w> W:通
\W> ☻:通
\s> \\\
:通
\S>乙:通
\d> 4:通
\D> G:通
(A)\1> AA:通
\k&所述; N>(小于; N>一个?) > AA:通
\1>(小于; N>一个?) AA:通
(A)\1\2>(小于; N> B') ABAB:通
(小于; N>一种?)(二)\1\2> ABBA:通
(A(B))\1\2> ababb:通
(A(B)(C(D)))\1\2\3\4> abcdabcdbcdd:通
a\0>答:通
AB * GT;答:通
AB +> AB | BB:通
AB? >答:通
AB {2}> ABB:通
AB {2}> abbbbbbbbb:通
AB {2,3}> ABB:通
AB *? > ABB:通
AB +? > abbbbb:通
AB? >答:通
AB {2}? > ABB:通
AB {2}? > abbbbbbbbb:通
AB {2,3}? > AB | BB:通
/用户(?:?/(小于号> \d +))? > /用户/ 77:通
传递52/52测试。


解决方案

例如见的Using正则表达式生成字符串,而不是与它们匹配



你也可以看看 http://en.wikipedia.org/特别是在一节接受并生成模式维基/ Deterministic_finite-state_machine



为别人注意到你需要从你的正则表达式,然后创建一个DFA 。生成使用该DFA你的字符串



你的正则表达式转换为DFA,首先生成NFA(例如见的 http://lambda.uta.edu/cse5317/spring01/notes/node9.html ),然后转换成NFA到DFA。



我看到的最简单的方法是使用该解析器生成程序。我不认为Django的做到这一点。



希望这有助于。


Given a regex pattern, I'm trying to find a string that matches it. Similar to how Django reverses them, but in C#. Are there any pre-made C# libraries that do this?


Edit: Moving this project to Google code pretty soon.

Current Test Results

^abc$                     > abc                  : pass
\Aa                       > a                    : pass
z\Z                       > z                    : pass
z\z                       > z                    : pass
z\z                       > z                    : pass
\G\(a\)                   > \(a\)                : pass
ab\b                      > ab                   : pass
a\Bb                      > ab                   : pass
\a                        >                     : pass
[\b]                      >                    : pass
\t                        > \t                   : pass
\r                        > \r                   : pass
\v                        > ♂                    : pass
\f                        > \f                   : pass
\n                        > \n                   : pass
\e                        > ←                    : pass
\141                      > a                    : pass
\x61                      > a                    : pass
\cC                       > ♥                    : pass
\u0061                    > a                    : pass
\\                        > \\                   : pass
[abc]                     > a                    : pass
[^abc]                    > î                    : pass
[a-z]                     > a                    : pass
.                         > p                    : pass
\w                        > W                    : pass
\W                        > ☻                    : pass
\s                        > \n                   : pass
\S                        > b                    : pass
\d                        > 4                    : pass
\D                        > G                    : pass
(a)\1                     > aa                   : pass
(?<n>a)\k<n>              > aa                   : pass
(?<n>a)\1                 > aa                   : pass
(a)(?<n>b)\1\2            > abab                 : pass
(?<n>a)(b)\1\2            > abba                 : pass
(a(b))\1\2                > ababb                : pass
(a(b)(c(d)))\1\2\3\4      > abcdabcdbcdd         : pass
a\0                       > a                    : pass
ab*                       > a                    : pass
ab+                       > abbb                 : pass
ab?                       > a                    : pass
ab{2}                     > abb                  : pass
ab{2,}                    > abbbbbbbbb           : pass
ab{2,3}                   > abb                  : pass
ab*?                      > abb                  : pass
ab+?                      > abbbbb               : pass
ab??                      > a                    : pass
ab{2}?                    > abb                  : pass
ab{2,}?                   > abbbbbbbbb           : pass
ab{2,3}?                  > abbb                 : pass
/users(?:/(?<id>\d+))?    > /users/77            : pass
Passed 52/52 tests.

解决方案

see for example Using Regex to generate Strings rather than match them

also you can take a look at http://en.wikipedia.org/wiki/Deterministic_finite-state_machine especially at "Accept and Generate modes" section.

as others noted you will need to create a DFA from your regular expression and then generate your strings using this DFA.

to convert your regular expression to DFA, generate NFA first (see for example http://lambda.uta.edu/cse5317/spring01/notes/node9.html) and then convert NFA to DFA.

the easiest way i see is to use a parser generator program for that. i do not think django does this.

hope this helps.

这篇关于算法查找字符串一个特定的正则表达式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆