使用正则表达式生成随机数据的方法
前端  /  管理员 发布于 7年前   244
从正则表达式生成随机数据
项目地址
https://github.com/GitHub-Laziji/reverse-regexp
安装
git clone https://github.com/GitHub-Laziji/reverse-regexp.gitcd reverse-regexpmvn install
<dependency> <groupId>org.laziji.commons</groupId> <artifactId>reverse-regexp</artifactId> <version>1.0-SNAPSHOT</version></dependency>
使用
随机字符语法
支持大部分正则表达式的匹配语法
重复打印语法
与正则表达式的重复匹配语法相同
其他语法
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] [email protected]
[true] [email protected]
[true] [email protected]
[true] [email protected]
[true] [email protected]
[true] [email protected]
[true] [email protected]
[true] [email protected]
[true] [email protected]
[true] [email protected]手机号 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] https://a_fl.thx/4_4
[true] http://v3YTuJ0Gu-5z8.JGhpdLe/V2j/ndL-UO
[true] https://FuMG-gafEc.R2FRRtLyX/ahg
[true] https://5phYVK9.wh7vl9z3AAZVg.z-yQSiMTdQw8S9-/WKCd
[true] https://T9dntbI.4Su8vxYhCr6?T85gV=R6TYtm&1c97x=nCTyA5
[true] http://qHyDgqBtYwq6Stg8.I6gb-M_ripkiEafK?ZLxy=Itny&RmsTX=X7KEuW&bwuk2=frcjO6
[true] http://UJ.Yb3foKTJKy-uqKUl.ZXrrFUk9K/Dcymu?W2tasq=oqzS&hYGWD=MF6l&FMHOi=F6ct8T
[true] https://BY_iS.tnlclAvxMkuO.T90G5XETj/cQncGI
[true] http://hG.li8Nzv.uaGokB/CTnrqp
[true] https://huZmN.v-LtoY/Dyl/peq?NUmt=__QDXG&8GBY=wZ8M&AkKZl4=8NZfEt
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号