基于正则表达式生成随机字符串? [英] Generate random string based on Regex?
本文介绍了基于正则表达式生成随机字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有一种方法可以从正则表达式生成随机字符串,例如:
I wonder if there is a way to generate random string from a regex like:
/[a-z0-9]{5}/.to_s
#=> "dsar3"
我找到了randexp
( https://github.com/benburkert/randexp ),但似乎无法使用像上面这样的基本示例,反正我觉得它被遗弃了.
I found randexp
(https://github.com/benburkert/randexp) but it seems to not work with a basic example like above and anyway I feel it's left abandoned.
有人吗?
推荐答案
Perl有一个可以执行此操作的CPAN模块.它通过将正则表达式转换为生成语法来工作.该概念可能可以适用于Ruby,但这会花费一点功夫.
Perl has a CPAN module that can do this. It works by converting the regex into a generative grammar. The concept can probably be adapted to Ruby, but would be a little work.
请参见 http://metacpan.org/pod/Parse::RandGen 和 http://metacpan.org/pod/Parse::RandGen::Regexp
这篇关于基于正则表达式生成随机字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文