南通颐猩文化传播有限公司

當(dāng)前位置:首頁(yè) >  站長(zhǎng) >  編程技術(shù) >  正文

使用正則表達(dá)式生成隨機(jī)數(shù)據(jù)的方法

 2020-12-24 16:42  來(lái)源: 腳本之家   我來(lái)投稿 撤稿糾錯(cuò)

  域名預(yù)訂/競(jìng)價(jià),好“米”不錯(cuò)過(guò)

這篇文章主要介紹了使用正則表達(dá)式生成隨機(jī)數(shù)據(jù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

從正則表達(dá)式生成隨機(jī)數(shù)據(jù)

項(xiàng)目地址

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>

使用

隨機(jī)字符語(yǔ)法

支持大部分正則表達(dá)式的匹配語(yǔ)法

\d 數(shù)字, 相當(dāng)于[0-9]

\w 數(shù)字、字母加下劃線, 相當(dāng)于[0-9a-zA-Z_]

\s 空白字符, 只包含空格和制表符

. 除\n和\r以外的任意字符, 生成隨機(jī)字符時(shí)只在ascii碼0~255之間生成

[a-zA-Z甲乙] 區(qū)間, 不支持^語(yǔ)法

以及其他字符

重復(fù)打印語(yǔ)法

與正則表達(dá)式的重復(fù)匹配語(yǔ)法相同

? 隨機(jī)生成0個(gè)或1個(gè)字符

* 隨機(jī)生成0個(gè)以上字符, 默認(rèn)最多16個(gè)

+ 隨機(jī)生成1個(gè)以上字符, 默認(rèn)最多16個(gè)

{n} 生成n個(gè)字符

{n,} 隨機(jī)生成n~個(gè)字符, 默認(rèn)最多max(16,n)個(gè)

{n,m} 隨機(jī)生成n~m個(gè)字符

其他語(yǔ)法

| 或語(yǔ)法, 例如aaa|bbb|ccc隨機(jī)生成aaa或bbb或ccc, 概率相等

() 支持括號(hào)

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}", "手機(jī)號(hào)");
random("-?[1-9]\\d*\\.\\d+", "浮點(diǎn)數(shù)");
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})?", "網(wǎng)址");
}

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

手機(jī)號(hào) 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

浮點(diǎn)數(shù) -?[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

網(wǎng)址 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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

來(lái)源:腳本之家

鏈接:https://www.jb51.net/article/179726.htm

申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

相關(guān)標(biāo)簽
正則表達(dá)式

相關(guān)文章

熱門排行

信息推薦