使用Regex生成字符串而不是匹配它们 [英] Using Regex to generate Strings rather than match them

查看:112
本文介绍了使用Regex生成字符串而不是匹配它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Java实用程序,它可以帮助我生成大量数据以进行性能测试。能够为字符串指定一个正则表达式以便我的生成器吐出与之匹配的东西,这真的真的很酷。有什么东西已经烘烤,我可以用来做这个吗?或者是否有一个图书馆让我大部分都在那里?

I am writing a Java utility which helps me to generate loads of data for performance testing. It would be really cool to be able to specify a regex for Strings so that my generator spits out things which match this. Is there something out there already baked which I can use to do this? Or is there a library which gets me most of the way there?

谢谢

推荐答案

修改:

正如评论中所述,Google Code上有一个图书馆可以实现这一目标:
http://code.google.com/p/xeger

As mentioned in the comments, there is a library available at Google Code to acheive this: http://code.google.com/p/xeger

另请参阅 https://github.com/mifmif/Generex 按照 Mifmif 的建议

原始邮件:

首先,有足够复杂的正则表达式,我相信这是不可能的。但是你应该可以将一些东西组合在一起以获得简单的正则表达式。

Firstly, with a complex enough regexp, i believe this can be impossible. But you should be able to put something together for simple regexps.

如果你看一下java.util.regex.Pattern类的源代码,你可以'将看到它使用Node实例的内部表示。每个不同的模式组件都有自己的Node子类实现。这些节点被组织成一个树。

If you take a look at the source code of the class java.util.regex.Pattern, you'll see that it uses an internal representation of Node instances. Each of the different pattern components have their own implementation of a Node subclass. These Nodes are organised into a tree.

通过生成遍历此树的访问者,您应该能够调用重载的生成器方法或某种类似于构建某些东西的构建器一起。

By producing a visitor that traverses this tree, you should be able to call an overloaded generator method or some kind of Builder that cobbles something together.

这篇关于使用Regex生成字符串而不是匹配它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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