加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – 生成正则表达式

发布时间:2020-12-14 06:32:31 所属栏目:百科 来源:网络整理
导读:通常在我们的工作中,我们在捕获或匹配操作中使用正则表达式 但是,可以使用正则表达式 – 至少手动 – 来生成与正则表达式匹配的合法句子.当然,一些正则表达式可以匹配无限长的句子,例如表达式. . 我有一个问题可以通过使用正则表达式句子生成算法来解决. 在
通常在我们的工作中,我们在捕获或匹配操作中使用正则表达式

但是,可以使用正则表达式 – 至少手动 – 来生成与正则表达式匹配的合法句子.当然,一些正则表达式可以匹配无限长的句子,例如表达式. .

我有一个问题可以通过使用正则表达式句子生成算法来解决.

在伪代码中,它将运行如下:

re = generate("foo(bar|baz)?",max_match = 100);  #Don't give me more than 100 results
assert re == ("foobar","foobaz","foo");

什么算法会为我执行此操作?

Microsoft为此提供了基于SMT的免费(MSRL许可)“Rex”工具: http://research.microsoft.com/en-us/downloads/7f1d87be-f6d9-495d-a699-f12599cea030/

来自“Rex:符号正则表达式资源管理器”的介绍部分:

We translate (extended) regular expressions or regexes [5] into a symbolic representation of finite automata called SFAs. In an SFA,moves are labeled by formulas representing sets of characters rather than individual characters. An SFA A is translated into a set of (recursive) axioms that describe the acceptance condition for the strings accepted by A and build on the representation of strings as lists.

由于SMT求解器可以在一定大小范围内输出所有可能的解决方案,因此这可能接近您正在寻找的内容.

在更加统计和不太正式的方面,CPAN的Regexp :: Genex模块也可以正常工作:http://search.cpan.org/dist/Regexp-Genex/

您可以使用以下内容:

#!/usr/bin/env perl
use Regexp::Genex ':all';
my $hits = 100;
my $re = qr/[a-z](123|456)/;
local $Regexp::Genex::DEFAULT_LEN = length $re;
my %seen;
while ((time - $^T) < 2) {
    @seen{strings($re)} = ();
    $Regexp::Genex::DEFAULT_LEN++;
}
print "$_n" for (sort %seen)[0..$hits-1];

根据需要调整时间和样本大小.希望这可以帮助!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读