|
|
目录 ?) Z5 t& f- U' I! T
- p# B; A& h" G) P" `
- m/ a9 J9 R. j1 j- 写在前面
5 O, q G" d5 v# K: f - 1、整数! H* X: k, x; f
- 2、正整数( D0 j+ {7 E7 w" e- a) x. R9 o
- 3、负整数
9 D# a7 M4 V4 i; s" g1 A7 ] - 4、数字/ s4 m4 {3 [# N; R
- 5、正数(正整数 + 0)
$ p; p$ S7 o1 N5 u2 m; w; m - 6、负数(负整数 + 0)
9 y; g3 `! m) q - 7、浮点数 G- Y0 ~" _9 Z5 S( ~
- 8、正浮点数+ r2 g% Z& E2 \6 o
- 9、负浮点数
6 j1 d. l% v7 `$ Y6 M0 _3 W: x - 10、浮点数- J9 ?7 N, E/ H/ |# b1 G
- 11、非负浮点数(正浮点数 + 0), W) U" A# H6 W. u
- 12、非正浮点数(负浮点数 + 0)0 \0 ?3 w$ V" k7 ?; }
- 13、邮箱地址. g& U% O3 R+ K6 Z% g8 |' z. r
- 14、颜色值匹配
" M& |; k( M! e+ V - 15、url匹配! `1 f% J5 a1 H4 `" i3 B- K3 n# b
- 16、纯仅中文字符0 u+ p" V Y( d6 G3 F" p* _8 L
- 17、仅ACSII字符
" Z1 m$ P* w7 s+ p, v - 18、邮政编码3 n. S9 o. W, f4 [6 h# V
- 19、国内手机号码
5 I0 f, z" f1 t" J - 20、IP V4 地址
! q! T3 V) n! q, M/ j1 X - 21、非空字符
# J( Z; w9 ~5 S+ D& t. t# _! ?- p - 22、图片后缀
. {$ U, o0 i T$ C) ^) R* u - 23、音频后缀
' }7 Z3 k# T' a$ z; o- |/ r' S6 `1 w - 24、视频后缀
! c# M: u7 o. W: @7 r3 Z$ B! J( w - 25、压缩文件后缀0 {% T# q0 u! ~
- 26、日期格式 2 Y# }9 Y1 u% m1 [$ j- K. d' ?7 A4 L
- 27、日期和时间格式
6 J/ {5 t" r8 s, N/ b - 28、QQ号码7 X$ J( ]( F' ?; H1 n- D# K; D
- 29、电话号码的函数(包括验证国内区号,国际区号,分机号)/ v: g& B4 U: B1 d# ~
- 30、用户名注册' B3 t K- h9 ?# d0 t& Q1 n/ E
- 31、字母数字组合* V( r) R3 \ Z+ k
- 32、纯字母
) n. s+ l+ s' o. z0 L* N; D- d6 N - 33、纯大写字母$ A! q- N: \: a2 ~
- 34、纯小写字母
" m7 ?, W9 Z& i" g& w - 35、第二代身份证号码匹配0 ^+ J* o- n0 w J. m8 x% u+ V' c
- 36、数字或字母
/ @+ L9 a' I, c5 \* x 3 p: l8 z& a& U) d# e7 Y6 n
写在前面
$ G" F. Q% B3 v$ Z X( N+ {! Z3 U9 V1 [" |, v+ ^8 Q
我们在日常的Java开发中,经常需要处理一些字符串,这个时候正则表达式是非常有用的。几乎在所有的编程语言中都支持正则表达式。以下我将压箱底多年的干货搬出来给大家参考,都是我们日常使用频次比较高的正则表达式,希望能能大大提高你的工作效率。如果本文对大家有帮助,大家可以关注“Tom弹架构”,后续会连载正则表达式的基础知识。% o" B- b* `( R3 N1 x' l+ u% N$ Y5 k
$ S" g) c; S. G" ~7 V$ A
1、整数1 E. `( L2 p% x& ^0 r) C: ]8 B
) O \5 N/ }2 F6 F* x$ A$ A! e - public static final String intege = "^-?[1-9]\\d*$/"; //整数 /** 正例 */ System.out.println(Pattern.matches(intege,"123")); // true System.out.println(Pattern.matches(intege,"-123")); // true /** 反例 */ System.out.println(Pattern.matches(intege,"abc")); // false System.out.println(Pattern.matches(intege,"0")); // false
复制代码 2、正整数
8 H: k: S- y: z
( N7 a, H) a: U/ s# A - public static final String intege1 = "^[1-9]\\d*$/"; //正整数 // 正例 System.out.println(Pattern.matches(intege1,"123")); // true // 反例 System.out.println(Pattern.matches(intege1,"-123")); // false System.out.println(Pattern.matches(intege1,"0")); // false
复制代码 3、负整数
8 Q! T# @5 g0 }: T \) N- L! C4 k" x* i E4 c& n" w; O
 - public static final String intege2 = "^-[1-9]\\d*$/"; //负整数 // 正例 System.out.println(Pattern.matches(intege2,"-123")); // true // 反例 System.out.println(Pattern.matches(intege2,"123")); // false System.out.println(Pattern.matches(intege2,"0")); // false
复制代码 4、数字
( J6 q f' E) X4 X5 n u* T( h! k4 X* {7 p( B8 ^
 - public static final String num = "^([+-]?)\\d*\\.?\\d+$/"; //数字 // 正例 System.out.println(Pattern.matches(num,"123")); // true System.out.println(Pattern.matches("0")); // true // 反例 System.out.println(Pattern.matches(num,"a123")); // false
复制代码 5、正数(正整数 + 0)+ J5 Q$ c( ~5 y( c) O, `
- public static final String num1 = "^[1-9]\\d*|0$/"; //正数(正整数 + 0) // 正例 System.out.println(Pattern.matches(num1,"123")); // true System.out.println(Pattern.matches(num1,"0")); // true // 反例 System.out.println(Pattern.matches(num1,"-123")); // false
复制代码 6、负数(负整数 + 0)( n/ p$ j& W5 w3 Q9 e
3 a& _8 ?+ r6 s2 }2 q* _9 K: g
- public static final String num2 = "^-[1-9]\\d*|0$/"; //负数(负整数 + 0) // 正例 System.out.println(Pattern.matches(num2,"-123")); // true System.out.println(Pattern.matches(num2,"0")); // true // 反例 System.out.println(Pattern.matches(num2,"123")); // false
复制代码 7、浮点数
5 L6 a8 c6 R2 {! F
8 h# L6 t; [3 V. ^: _- public static final String decmal = "^([+-]?)\\d*\\.\\d+$/"; //浮点数 // 正例 System.out.println(Pattern.matches(decmal,"-0.1")); // true System.out.println(Pattern.matches(decmal,"0.1")); // true // 反例 System.out.println(Pattern.matches(decmal,"a.b")); // false
复制代码 8、正浮点数1 j5 w5 B* ]" a
3 M6 b' i- j6 y: S( m
 - public static final String decmal1 = "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$"; //正浮点数 // 正例 System.out.println(Pattern.matches(decmal1,"0.1")); // true // 反例 System.out.println(Pattern.matches(decmal1,"-0.1")); // false
复制代码 9、负浮点数
k+ D, g. j5 W! |$ p! V
4 L8 v& J1 w* Z1 m+ {7 V* V- public static final String decmal2 = "^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)$"; //负浮点数 // 正例 System.out.println(Pattern.matches(decmal2,"-0.1")); // true // 反例 System.out.println(Pattern.matches(decmal2,"0.1")); // false
复制代码 10、浮点数+ h' R6 w+ c: w' ^/ A; v
Q) v) F$ r/ } ^5 L" Z
 - public static final String decmal3 = "^-?([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0)$";//浮点数 // 正例 System.out.println(Pattern.matches(decmal3,"-0.1")); // true System.out.println(Pattern.matches(decmal3,"0.1")); // true // 反例 System.out.println(Pattern.matches(decmal3,"a.b")); // false
复制代码 11、非负浮点数(正浮点数 + 0)
8 }' X6 r6 x, O. E0 s" z; A
* L) J2 h$ C5 `* V3 v* r1 E- public static final String decmal4 = "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0$"; //非负浮点数(正浮点数 + 0) // 正例 System.out.println(Pattern.matches(decmal4,"0.1")); // true // 反例 System.out.println(Pattern.matches(decmal4,"-0.1")); // false
复制代码 12、非正浮点数(负浮点数 + 0)5 _$ R1 f; p: M/ h
, c6 r0 q$ d6 S- q- public static final String decmal5 = "^(-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*))|0?.0+|0$"; //非正浮点数(负浮点数 + 0) // 正例 System.out.println(Pattern.matches(decmal5,"-0.1")); // true // 反例 System.out.println(Pattern.matches(decmal5,"0.1")); // false
复制代码 13、邮箱地址; A4 v+ S6 U" u# p2 n# M7 g
2 g( y# J- p3 C- public static final String email = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$"; //邮件 // 正例 System.out.println(Pattern.matches(email,"tom@gupaoedu.com")); // true // 反例 System.out.println(Pattern.matches(email,"tom.gupaoedu.com")); // false
复制代码 14、颜色值匹配" x& O# }1 K, k$ C% U
3 ~% L9 f6 Q" X! V% ~* ?# A9 L
 - public static final String color = "^[a-fA-F0-9]{6}$"; //颜色 // 正例 System.out.println(Pattern.matches(color,"ffffff")); // true System.out.println(Pattern.matches(color,"FFFFFF")); // true // 反例 System.out.println(Pattern.matches(color,"#FFFFFF")); // false System.out.println(Pattern.matches(color,"white")); // false
复制代码 15、url匹配
$ V' J& q/ c) H7 X/ R: t
* h' G8 a- g& H# t- public static final String url = "^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-.\\/?%&=]*)?$"; //url // 正例 System.out.println(Pattern.matches(url,"http://www.xxx.com")); // true System.out.println(Pattern.matches(url,"https://www.xxx.com")); // true System.out.println(Pattern.matches(url,"www.xxx.com")); // true // 反例 System.out.println(Pattern.matches(url,"abcd")); // false
复制代码 16、纯仅中文字符. s+ ~6 Z, ?. b+ f$ s3 `2 ]5 D
( A) w' X: W, r0 s2 H
 - public static final String chinese = "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$"; //仅中文 // 正例 System.out.println(Pattern.matches(chinese,"汤姆弹架构")); // true // 反例 System.out.println(Pattern.matches(chinese,"Tom弹架构")); // false
复制代码 17、仅ACSII字符
) V/ ]" T; }! g Z0 U* a
5 \( n. b0 B d% d$ D2 | - public static final String ascii = "^[\\x00-\\xFF]+$"; //仅ACSII字符 // 正例 System.out.println(Pattern.matches(ascii,"abc123")); // true // 反例 System.out.println(Pattern.matches(ascii,"にそ①②③")); // false
复制代码 18、邮政编码
* W7 f9 l. k- E* Q9 k) j7 R0 O( r
 - public static final String zipcode = "^\\d{6}$"; //邮编 // 正例 System.out.println(Pattern.matches(zipcode,"100000")); // true // 反例 System.out.println(Pattern.matches(zipcode,"1000000")); // false
复制代码 19、国内手机号码
. g/ k! ?" \$ h
: t: d% V$ G" P- v* B- public static final String mobile = "^(13|15|16|17|18)[0-9]{9}$"; //手机 // 正例 System.out.println(Pattern.matches(zipcode,"13800138000")); // true // 反例 System.out.println(Pattern.matches(zipcode,"19900010002")); // false
复制代码 20、IP V4 地址; i o A% a7 w' g, s% s1 ] X/ e
, j# X/ v7 Z& B, o- public static final String ip4 = "^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$"; //ip地址 // 正例 System.out.println(Pattern.matches(zipcode,"127.0.0.1")); // true // 反例 System.out.println(Pattern.matches(zipcode,"aa.bb.cc.dd")); // false
复制代码 21、非空字符' L1 ~. L7 b7 J# A H4 \% T$ w* u
4 O1 k- T/ z5 C0 w( P% ]3 K/ c
- public static final String notempty = "^\\S+$"; //非空 // 正例 System.out.println(Pattern.matches(notempty," abc ")); // true // 反例 System.out.println(Pattern.matches(notempty,"")); // false
复制代码 22、图片后缀' N" X/ ]* z& Q, Y* u
6 W/ q/ g. v: a: `/ p, u, H; y
 - public static final String picture = "(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga|JPG|BMP|GIF|ICO|PCX|JPEG|TIF|PNG|RAW|TGA)$"; //图片 // 正例 System.out.println(Pattern.matches(picture,"tom.jpg")); // true // 反例 System.out.println(Pattern.matches(picture,"tom.txt"")); // false
复制代码 23、音频后缀
( F, R+ B, ? H) N" m, m4 n
2 S K$ W7 l6 [, [- public static final String audio = "(.*)\\.(mp3|wma|mid|midi|wav|vqf|MP3|WMA|MID|MIDI|WAV|VQF)$"; //音频 // 正例 System.out.println(Pattern.matches(audio,"tom.mp3")); // true // 反例 System.out.println(Pattern.matches(audio,"tom.txt"")); // false
复制代码 24、视频后缀: `, A) Q8 g, H% S% b! ]# \5 Y
2 Q' F8 }4 Z6 @7 R: Y
- public static final String video = "(.*)\\.(rm|3gp|mp4|rmvb|avi|wmv|flv|vob|exe|mkv|swf|RM|3GP|MP4|RMVB|AVI|WMV|FLV|VOB|EXE|MKV|SWF)$"; // 视频格式 // 正例 System.out.println(Pattern.matches(video,"tom.mp4")); // true // 反例 System.out.println(Pattern.matches(video,"tom.txt"")); // false
复制代码 25、压缩文件后缀
, g$ V1 A) }6 \7 v7 l+ k0 `/ R3 Q5 t+ o: d. }, P
- public static final String rar = "(.*)\\.(rar|zip|7zip|tgz|RAR|ZIP|7ZIP|TGZ)$"; //压缩文件 // 正例 System.out.println(Pattern.matches(rar,"tom.zip")); // true // 反例 System.out.println(Pattern.matches(rar,"tom.txt"")); // false
复制代码 26、日期格式 0 U9 j5 G3 D7 B: R
2 O, z$ S9 `, W5 Q( z P- public static final String date = "^\\d{4}(\\-|\\/|\\.)\\d{1,2}\\1\\d{1,2}$"; //日期 // 正例 System.out.println(Pattern.matches(date,"2024-10-24")); // true System.out.println(Pattern.matches(date,"2024/10/24")); // true // 反例 System.out.println(Pattern.matches(date,"2024年10月24日"")); // false
复制代码 27、日期和时间格式
) E8 ^4 O( E* \. R, r$ I8 G( |1 h9 k: T
- public static final String datetime = "^\\d{4}(\\-|\\/|\\.)\\d{1,2}\\1\\d{1,2}(\\s\\d{2}:)?(\\d{2}:)?(\\d{2})?$"; //日期和时间 // 正例 System.out.println(Pattern.matches(datetime,"2024-10-24 23:59:59")); // true System.out.println(Pattern.matches(datetime,"2024/10/24 23:59:59")); // true // 反例 System.out.println(Pattern.matches(datetime,"2024年10月24日 23时59分59秒"")); // false
复制代码 28、QQ号码8 ]8 |7 H" a8 r G, \- u
8 ~- A& O) `( |# h8 B( C% h# n6 ` - public static final String qq = "^[1-9]*[1-9][0-9]*$"; //QQ号码 // 正例 System.out.println(Pattern.matches(qq,"123456")); // true // 反例 System.out.println(Pattern.matches(qq,"1234567890")); // false
复制代码 29、电话号码的函数(包括验证国内区号,国际区号,分机号)
1 M7 k* z8 M/ X! V; ]7 \. \* l5 [! V: I+ r' T8 p6 z6 Y s% u
- public static final String tel = "^(([0\\+]\\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?$"; //电话号码的函数(包括验证国内区号,国际区号,分机号) // 正例 System.out.println(Pattern.matches(tel,"010-1234567")); // true System.out.println(Pattern.matches(tel,"0100-12345678")); // true // 反例 System.out.println(Pattern.matches(tel,"13800138000")); // false
复制代码 30、用户名注册! [, Z) C3 l. I4 x
+ X, K( K# Y: Q5 S
 - public static final String username = "^[A-Za-z]\\w{5,}$"; //用来用户注册。匹配由数字、26个英文字母或者下划线组成的字符串 // 正例 System.out.println(Pattern.matches(username,"gupaoedutom")); // true // 反例 System.out.println(Pattern.matches(username,"tom@gupaoedu")); // false
复制代码 31、字母数字组合+ m3 `% T6 x& j; }" b
- public static final String allstring = "^\\w+$"; //字母数字组合 // 正例 System.out.println(Pattern.matches(allstring,"abc123")); // true // 反例 System.out.println(Pattern.matches(allstring,"abc123%^&")); // false
复制代码 32、纯字母
4 |7 ~; |& V' e- public static final String letter = "^[A-Za-z]+$"; //字母 // 正例 System.out.println(Pattern.matches(letter,"abc")); // true // 反例 System.out.println(Pattern.matches(letter,"abc123")); // false
复制代码 33、纯大写字母
) x* @: X0 L; U+ ^: k9 S- public static final String letter_u = "^[A-Z]+$"; //大写字母 // 正例 System.out.println(Pattern.matches(letter_u,"ABC")); // true // 反例 System.out.println(Pattern.matches(letter_u,"abc")); // false
复制代码 34、纯小写字母
' x" i; g- `/ j5 o! f- public static final String letter_l = "^[a-z]+$"; //小写字母 // 正例 System.out.println(Pattern.matches(letter_l,"abc")); // true // 反例 System.out.println(Pattern.matches(letter_l,"ABC")); // false
复制代码 35、第二代身份证号码匹配
' n( z4 h0 |1 Y+ l- public static final String idcard = "^[1-9]([0-9]{14}|[0-9]{17})$"; //身份证 // 正例 System.out.println(Pattern.matches(idcard,"100000201410241024")); // true // 反例 System.out.println(Pattern.matches(idcard,"1000002014102410240")); // false
复制代码 36、数字或字母! s5 e4 N0 U9 |. F. M% {6 t; F/ t. N, ]
- public static final String numOrStr = "^[A-Za-z0-9]+$";//数字或字母 // 正例 System.out.println(Pattern.matches(numOrStr,"abc123")); // true System.out.println(Pattern.matches(numOrStr,"abc")); // true System.out.println(Pattern.matches(numOrStr,"123")); // true // 反例 System.out.println(Pattern.matches(numOrStr,"脚本之家")); // false
复制代码 到此这篇关于36个正则表达式(开发效率提高80%)的文章就介绍到这了,更多相关正则表达式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
2 Z; \7 ]; g' O4 A* k5 @# s6 s" G5 @5 r1 A* b6 \6 n) U
来源:http://www.jb51.net/article/229226.htm
! P3 `2 Z; k' B' K: m免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|