如何从正则表达式生成与正则表达式匹配的文本? [英] How do I generate text matching a regular expression from a regular expression?

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

问题描述

是的,你读得对。我需要能够从正则表达式生成随机文本的东西。所以文本应该是随机的,但要与正则表达式匹配。它似乎不存在,但我可能是错的。



只是一个例子:该库能够获取' [ab ] * c '作为输入,并生成如下样本:


abc

abbbc

bac


等。



更新:我自己创造了一些东西:Xeger。查看 http://code.google.com/p/xeger/

解决方案

我刚刚创建了一个库来执行此操作。它位于此处: http://code.google.com/p/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:

abc
abbbc
bac

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);

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

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