如何从 Perl 正则表达式生成所有可能的排列?

发布于 2024-08-29 04:17:44 字数 429 浏览 2 评论 0原文

我知道您可以使用 globAlgorithm::Permute - 但是如何从正则表达式生成所有可能的排列呢?

我想做这样的事情:

@perms = permute( "/\s[A-Z][0-9][0-9]/" );
sub permute( $regex ) {
    # code - put all permutations of above regex in a list
    return @list;
}

I know you can generate all permutations from a list, using glob or Algorithm::Permute for example - but how do you generate all possible permutations from a regular expression?

I want to do like:

@perms = permute( "/\s[A-Z][0-9][0-9]/" );
sub permute( $regex ) {
    # code - put all permutations of above regex in a list
    return @list;
}

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

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

发布评论

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

评论(4

谜兔 2024-09-05 04:17:44

请参阅 高阶 Perl。考虑购买印刷书:它是一件艺术品。

CPAN

See Section 6.5 (PDF) in Higher Order Perl. Consider buying the print book: It is a work of art.

There is also Regexp::Genex on CPAN.

蓝天 2024-09-05 04:17:44

任何可能的实现都应该为生成的字符串考虑合理的最大长度。
如果该正则表达式中的任何位置有 +*,则可能性可能是无穷无尽的。
Regexp::Genex 考虑了这一点。

Any possibly implementation should have a reasonable maximum length in mind for the generated strings.
If there's a + or * anywhere in that regexp, the possibilities could be without end.
Regexp::Genex considers this.

掐死时间 2024-09-05 04:17:44

我遇到的解决方案都不能处理前瞻; Regexp::Genex 没有,这里的解决方案也没有:

http://www.mail-archive.com/[电子邮件受保护]/msg31051.html

虽然我同意 HOP是一本很棒的书,它实际上只处理“开箱即用”的正则表达式的一小部分。

如果有人知道处理前瞻的人,那就太好了:/

None of the solutions I've encountered handle lookaheads; Regexp::Genex doesn't, nor does the solution here:

http://www.mail-archive.com/[email protected]/msg31051.html

While I agree that HOP is an awesome book, it's really only dealing with a small subset of regexes "out-of-the-box".

If anybody knows of one that handles lookaheads, that'd be great :/

醉殇 2024-09-05 04:17:44

以防万一有人发现它有用:

*$ cat bitfizz.pl*
#!/usr/bin/perl 
use strict;
if ( ($#ARGV+1)!=2 ) { print "usage $0  \n"; }
my @r = &bitfizz( $ARGV[0], $ARGV[1] );
for(@r){ print "$_\n"; }
sub bitfizz() {
    $_[0]=join( ",", split(//, $_[0] ) );
    for( my $i=1; $i<=$_[1]; $i+=1 ) { $_=$_."{$_[0]}"; } 
    @r=glob( $_ );
}

那么你可以这样做:

*perl bitfizz.pl "01" 8*
00000000
00000001
00000010
00000011
00000100
--snip--

一个字节的所有位排列
或者

*perl bitfizz.pl "0123456789ABCDEF" 2*

例如

just in case anyone finds it useful:

*$ cat bitfizz.pl*
#!/usr/bin/perl 
use strict;
if ( ($#ARGV+1)!=2 ) { print "usage $0  \n"; }
my @r = &bitfizz( $ARGV[0], $ARGV[1] );
for(@r){ print "$_\n"; }
sub bitfizz() {
    $_[0]=join( ",", split(//, $_[0] ) );
    for( my $i=1; $i<=$_[1]; $i+=1 ) { $_=$_."{$_[0]}"; } 
    @r=glob( $_ );
}

then you can do:

*perl bitfizz.pl "01" 8*
00000000
00000001
00000010
00000011
00000100
--snip--

all permutations of a byte in bits
or

*perl bitfizz.pl "0123456789ABCDEF" 2*

for example

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