问题描述
是的,你读得对。我需要能够从正则表达式生成随机文本的东西。所以文本应该是随机的,但要与正则表达式匹配。它似乎不存在,但我可能是错的。只是一个例子:该库能够获取' [ab ] * c
'作为输入,并生成如下样本:
等。
更新:我自己创造了一些东西:Xeger。查看。
我刚刚创建了一个库来执行此操作。它位于此处:。使用前请仔细阅读说明。 (特别是指下载另一个必需的库。); - )
这是你使用它的方式:
String regex =[ab] {4,6} c;
Xeger generator = new Xeger(正则表达式);
String result = generator.generate();
断言result.matches(正则表达式);
Yup, you read that right. I needs something that is capable of generating random text from a regular expression. So the text should be random, but be matched by the regular expression. It seems it doesn't exist, but I could be wrong.
Just a an example: that library would be capable of taking '[ab]*c
' as input, and generate samples such as:
etc.
Update: I created something myself: Xeger. Check out http://code.google.com/p/xeger/.
I just created a library for doing this a minute ago. It's hosted here: http://code.google.com/p/xeger/. Carefully read the instructions before using it. (Especially the one referring to downloading another required library.) ;-)
This is the way you use it:
String regex = "[ab]{4,6}c";
Xeger generator = new Xeger(regex);
String result = generator.generate();
assert result.matches(regex);
这篇关于如何从正则表达式生成与正则表达式匹配的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!