基于正则表达式的随机文本生成器
Xeger能够这样做的:
String regex = "[ab]{4,6}c";
Xeger generator = new Xeger(regex);
String result = generator.generate();
assert result.matches(regex);
而不是从一个正则表达式开始,你应该看看写一个小的上下文*文法,这将允许你轻松地生成这样的随机文本。不幸的是,我知道没有任何工具会直接为你做,所以你需要自己做一些代码才能真正生成文本。如果你之前没有使用过语法,我建议你在继续之前阅读一下关于bnf格式和“编译器编译器”的内容......
我不知道任何,尽管它应该是可能的。通常的做法是编写一个语法而不是正则表达式,然后为每个非终端创建函数,随机决定要扩展哪个生产。如果您可以发布您想要生成的字符串类型的描述以及您正在使用的语言,我们可能会帮助您开始。
我们在Python中做了一些类似的,不久前我们编写了一个RegEx game。我们有一个约束条件,即正则表达式必须是随机生成的,选定的单词必须是真实的单词。您可以下载已完成的游戏EXE here和Python源代码here。
这里是一个片段:
def generate_problem(level):
keep_trying = True
while(keep_trying):
regex = gen_regex(level)
# print 'regex = ' + regex
counter = 0
match = 0
notmatch = 0
goodwords = []
badwords = []
num_words = 2 + level * 3
if num_words > 18:
num_words = 18
max_word_length = level + 4
while (counter < 10000) and ((match < num_words) or (notmatch < num_words)):
counter += 1
rand_word = words[random.randint(0,max_word)]
if len(rand_word) > max_word_length:
continue
mo = re.search(regex, rand_word)
if mo:
match += 1
if len(goodwords) < num_words:
goodwords.append(rand_word)
else:
notmatch += 1
if len(badwords) < num_words:
badwords.append(rand_word)
if counter < 10000:
new_prob = problem.problem()
new_prob.title = 'Level ' + str(level)
new_prob.explanation = 'This is a level %d puzzle. ' % level
new_prob.goodwords = goodwords
new_prob.badwords = badwords
new_prob.regex = regex
keep_trying = False
return new_prob
退房的RandExp红宝石的宝石。它只是以有限的方式做你想做的事。 (它不适用于任何可能的正则表达式,只有满足一些限制的正则表达式。)
它被感动了:http://github.com/benburkert/randexp – 2009-12-31 11:07:02
所有正则表达式都可以表示为上下文无关语法。有一个a nice algorithm already worked out用于从给定长度的任何CF*生随机句子。因此,将正则表达式上转换为cfg,应用算法,然后重击,就完成了。
算法的任何已知实现?这是一个长镜头? – Paralife 2008-11-08 00:10:24
我几年前在Perl中成功实现了它,并且它看到了“生产”的用法,所以我可能做对了。这一过程中最难的部分是了解论文中使用的符号。清除障碍,你是金。 – 2008-11-08 02:29:16
为时已晚,但它可以帮助新人,这里是一个有用的java library,对于使用正则表达式生成字符串提供许多功能(随机生成,在此基础上的指数生成的字符串,生成所有字符串。)检查出来here。
例子:
Generex generex = new Generex("[0-3]([a-c]|[e-g]{1,2})");
// generate the second String in lexicographical order that match the given Regex.
String secondString = generex.getMatchedString(2);
System.out.println(secondString);// it print '0b'
// Generate all String that matches the given Regex.
List<String> matchedStrs = generex.getAllMatchedStrings();
// Using Generex iterator
Iterator iterator = generex.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
// it print 0a 0b 0c 0e 0ee 0e 0e 0f 0fe 0f 0f 0g 0ge 0g 0g 1a 1b 1c 1e
// 1ee 1e 1e 1f 1fe 1f 1f 1g 1ge 1g 1g 2a 2b 2c 2e 2ee 2e 2e 2f 2fe 2f 2f 2g
// 2ge 2g 2g 3a 3b 3c 3e 3ee 3e 3e 3f 3fe 3f 3f 3g 3ge 3g 3g 1ee
// Generate random String
String randomStr = generex.random();
System.out.println(randomStr);// a random value from the previous String list
是的,存在软件,它可以生成一个随机匹配的正则表达式:
- Exrex,巨蟒
- Pxeger,使用Javascript
- regex-genex,哈斯克尔
- Xeger,J AVA
- Xeger,巨蟒
- Generex,爪哇
- rxrdg,C#
- String::Random,Perl的
- regldg,C
- paggern,PHP
- ReverseRegex,PHP
- randexp.js,使用Javascript
- EGRET,巨蟒/ C++
- MutRex,爪哇
- Fare,C#
- rstr,巨蟒
- randexp,红宝石
- goregen,去
- bfgex,爪哇
- regexgen,使用Javascript
- strgen,巨蟒
- random-string,爪哇
- regexp-unfolder,Clojure的
- string-random,哈斯克尔
- rxrdg,C#
- Regexp::Genex,Perl的
- StringGenerator,巨蟒
- strrand,去
- regen,去
- Rex,C#
- regexp-examples,红宝石
重复的问题:http://*.com/questions/22115/using-regex-to-generate-strings-rather-than-匹配他们 – 2012-11-18 02:12:27