生成与Java中的正则表达式匹配的字符串

时间:2013-05-01 11:35:53

标签: java regex

受“java use Regular Expressions to generate a string的启发”我想知道如何生成与Java中的正则表达式匹配的字符串。

/**
 * Returns a list of {@code count} strings which match the regular
 * expression {@code regexp}.
 *
 * @param regexp the regular expression
 * @param count the number of strings to return
 * @result a list of {@code count} strings matching {@code regexp}
 */
public static List<String> generate(final String regexp, final int count)
{
    // ...
}

1 个答案:

答案 0 :(得分:1)

您可以查看提供此功能的Xeger库。示例:

String regex = "[ab]{4,6}c";
Xeger generator = new Xeger(regex);
String result = generator.generate();
assert result.matches(regex);