2013-05-01 52 views
0

受「java use Regular Expressions to generate a string」啓發我想知道如何生成與Java中的正則表達式匹配的字符串。生成與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

你可以看看Xeger庫,它提供了這個功能。

例子:

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

謝謝。你怎麼知道如何手動執行它的一般想法? – 2013-05-01 11:41:09

+0

不,我不知道,但你可以聯繫[Wilfred Springer](http://stackoverflow.com/users/136476/wilfred-springer)。他是編寫這個庫的人 – 2013-05-01 11:51:55

+0

查看Xeger的源代碼如何? – 2013-05-01 12:01:26