如何从 Perl 正则表达式生成所有可能的排列?
我知道您可以使用 glob 或 Algorithm::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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请参阅 高阶 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.
任何可能的实现都应该为生成的字符串考虑合理的最大长度。
如果该正则表达式中的任何位置有
+
或*
,则可能性可能是无穷无尽的。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.
我遇到的解决方案都不能处理前瞻; 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 :/
以防万一有人发现它有用:
那么你可以这样做:
一个字节的所有位排列
或者
例如
just in case anyone finds it useful:
then you can do:
all permutations of a byte in bits
or
for example