这个还真挺有意思,对应自动化测试时非常有用,尤其是在写自动化测试的case的时候,如果可以根据规则进行随机生成,测试工作将会大大提高效率.因为我们以前都是用正则进行校验,并没有做类似反向的操作.然后google之
果然有这么个东西.不多说贴代码...这个鬼叫
Xeger
package com.qunar.xeger;
import nl.flotsam.xeger.Xeger;
public class XegerTest {
public static void main(String[] args) {
String regex = "[0-9]{3}\\.[0-9]{3}\\.[0-9]{3}";
//三位数
Xeger generator = new Xeger(regex);
for (int i = 0; i < 2000; i++) {
String result = generator.generate();
System.out.println(result);
assert result.matches(regex);
}
}
}
@H_404_100@结果如下,看来还是挺ok的 @H_404_100@@H_404_100@106.433.601 @H_404_100@676.317.893 @H_404_100@490.884.177 @H_404_100@187.327.513 @H_404_100@141.045.456 @H_404_100@365.366.232 @H_404_100@567.664.228 @H_404_100@624.774.216 @H_404_100@071.483.373 @H_404_100@089.802.138 @H_404_100@675.559.342 @H_404_100@253.742.791 @H_404_100@360.112.624 @H_404_100@678.587.655 @H_404_100@224.510.673 @H_404_100@145.617.038 @H_404_100@656.584.855 @H_404_100@132.857.253 @H_404_100@670.638.464 @H_404_100@852.831.675 @H_404_100@937.487.663 @H_404_100@357.717.132 @H_404_100@332.717.650 @H_404_100@321.358.373 @H_404_100@231.876.187 @H_404_100@544.841.281 @H_404_100@756.875.649 @H_404_100@416.405.727 @H_404_100@547.757.870 @H_404_100@459.842.682 @H_404_100@226.621.249 @H_404_100@469.582.654 @H_404_100@381.678.130 @H_404_100@590.769.143 @H_404_100@