如何生成与给定正则表达式匹配的随机字符串? [英] How to generate random strings that match a given regexp?

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

问题描述

与正则表达式匹配的随机字符串

不,不是.我正在寻找一种可以实际实现的简单且通用的方法.这比随机生成密码要困难得多.

No, it isn't. I'm looking for an easy and universal method, one that I could actually implement. That's far more difficult than randomly generating passwords.

我想创建一个使用正则表达式的应用程序,并显示与该表达式匹配的10个随机生成的字符串.它应该可以帮助人们更好地了解他们的正则表达式,并确定他们是否足够安全以进行验证.有人知道这样做的简单方法吗?

I want to create an application that takes a regular expression, and shows 10 randomly generated strings that match that expression. It's supposed to help people better understand their regexps, and to decide i.e. if they're secure enough for validation purposes. Does anyone know of an easy way to do that?

一个显而易见的解决方案是编写(或窃取)一个正则表达式解析器,但这似乎让我头疼.

One obvious solution would be to write (or steal) a regexp parser, but that seems really over my head.

我再说一遍,我正在寻找一种简单通用的方法.

I repeat, I'm looking for an easy and universal way to do that.

编辑:蛮力方法是不可能的.假设随机字符串为[a-z0-9]{10}且每秒进行一百万次迭代,则需要

Brute force approach is out of the question. Assuming the random strings would just be [a-z0-9]{10} and 1 million iterations per second, it would take 65 years to iterate trough the space of all 10-char strings.

推荐答案

将正则表达式解析为 DFA ,然后随机遍历DFA,直到最终进入接受状态,并为每个过渡输出一个字符.每次步行都会产生一个与表达式匹配的新字符串.

Parse your regular expression into a DFA, then traverse your DFA randomly until you end up in an accepting state, outputting a character for each transition. Each walk will yield a new string that matches the expression.

这对于不是真正正则的正则"表达式不起作用,例如带有反向引用的表达式.这取决于你追求哪种表情.

This doesn't work for "regular" expressions that aren't really regular, though, such as expressions with backreferences. It depends on what kind of expression you're after.

这篇关于如何生成与给定正则表达式匹配的随机字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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