当前位置: 移动技术网 > IT编程>开发语言>正则 > 使用正则表达式生成随机数据的方法

使用正则表达式生成随机数据的方法

2020年03月09日  | 移动技术网IT编程  | 我要评论

从正则表达式生成随机数据

项目地址

https://github.com/github-laziji/reverse-regexp

安装

git clone https://github.com/github-laziji/reverse-regexp.git
cd reverse-regexp
mvn install
<dependency>
  <groupid>org.laziji.commons</groupid>
  <artifactid>reverse-regexp</artifactid>
  <version>1.0-snapshot</version>
</dependency>

使用

随机字符语法

支持大部分正则表达式的匹配语法

  • \d 数字, 相当于[0-9]
  • \w 数字、字母加下划线, 相当于[0-9a-za-z_]
  • \s 空白字符, 只包含空格和制表符
  • . 除\n和\r以外的任意字符, 生成随机字符时只在ascii码0~255之间生成
  • [a-za-z甲乙] 区间, 不支持^语法
  • 以及其他字符

重复打印语法

与正则表达式的重复匹配语法相同

  • ? 随机生成0个或1个字符
  • * 随机生成0个以上字符, 默认最多16个
  • + 随机生成1个以上字符, 默认最多16个
  • {n} 生成n个字符
  • {n,} 随机生成n~个字符, 默认最多max(16,n)个
  • {n,m} 随机生成n~m个字符

其他语法

  • | 或语法, 例如aaa|bbb|ccc随机生成aaa或bbb或ccc, 概率相等
  • () 支持括号
public class maintest {

  @test
  public void test() throws regexpillegalexception, uninitializedexception, typenotmatchexception {
    
    random("\\w{6,12}@[a-z0-9]{3}\\.(com|cn)", "邮箱");
    random("1(3|5|7|8)\\d{9}", "手机号");
    random("-?[1-9]\\d*\\.\\d+", "浮点数");
    random("https?://[\\w-]+(\\.[\\w-]+){1,2}(/[\\w-]{3,6}){0,2}(\\?[\\w_]{4,6}=[\\w_]{4,6}(&[\\w_]{4,6}=[\\w_]{4,6}){0,2})?", "网址");
  }
  
  private void random(string expression, string title)
      throws regexpillegalexception, typenotmatchexception, uninitializedexception {
    
    system.out.println(title + " " + expression);
    node node = new ordinarynode(expression);
    pattern pattern = pattern.compile(node.getexpression());
    for (int i = 0; i < 10; i++) {
      string data = node.random();
      system.out.println("[" + pattern.matcher(data).matches() + "] " + data);
    }
    system.out.println();
  }

}

输出
邮箱 \w{6,12}@[a-z0-9]{3}\.(com|cn)
[true] 19cz8eisna@9je.com
[true] xpv3wj@i3h.cn
[true] 6qdufy@1g9.com
[true] ivnzsma373@6zd.cn
[true] i5wix97@ffe.cn
[true] mwqa5sxq@g8j.cn
[true] huxicem1y0w@j98.cn
[true] 1joqwself@u1o.cn
[true] _q4qtvxpemfh@bds.com
[true] 3xfh33aa@6lh.cn

手机号 1(3|5|7|8)\d{9}
[true] 18263364656
[true] 17539493178
[true] 17452542895
[true] 15190699623
[true] 13441385631
[true] 15450856416
[true] 18651247283
[true] 13835809899
[true] 18595798569
[true] 17115703866

浮点数 -?[1-9]\d*\.\d+
[true] 8148340336.1501586550282701
[true] -3339660539.406
[true] -51.6120243661611419
[true] -731621835440468.9708278
[true] -27438753435.9137579
[true] 393811376.777268751417
[true] 3286498432415.3962664603
[true] -5299652275.9
[true] 216.93676279820770
[true] 34.36843273

网址 https?://[\w-]+(\.[\w-]+){1,2}(/[\w-]{3,6}){0,2}(\?[\w_]{4,6}=[\w_]{4,6}(&[\w_]{4,6}=[\w_]{4,6}){0,2})?
[true]
[true]
[true] https://fumg-gafec.r2frrtlyx/ahg
[true] https://5phyvk9.wh7vl9z3aazvg.z-yqsimtdqw8s9-/wkcd
[true] https://t9dntbi.4su8vxyhcr6?t85gv=r6tytm&1c97x=nctya5
[true]
[true]
[true] https://by_is.tnlclavxmkuo.t90g5xetj/cqncgi
[true]
[true] https://huzmn.v-ltoy/dyl/peq?numt=__qdxg&8gby=wz8m&akkzl4=8nzfet

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网