|
|
目录3 K J7 \7 n" P$ k# a% j
$ f- v1 j6 A- P# m- c
% P9 U8 M/ }- w o1 A3 n- 写在前面, G+ b& X2 f9 z9 ?! S0 m
- 1、整数
1 y+ K% ]! M- Q# ]) n- ?! J3 @8 t - 2、正整数4 k3 B y5 b3 y( n4 }. [
- 3、负整数7 P, M9 K3 l* [; M% C
- 4、数字/ f/ k7 c1 ~4 p9 r
- 5、正数(正整数 + 0)' a2 T: Y) a, y. r
- 6、负数(负整数 + 0)& a5 ^( f7 ?; t4 |* E! s( V2 T0 A
- 7、浮点数" |: s! N( ?- V8 i, f
- 8、正浮点数
% ~7 ?) y$ }0 z$ o, ?. e2 E0 a - 9、负浮点数' z( Y. l& c; F2 e7 y+ C% b
- 10、浮点数
9 D: i" z6 S8 m: \1 M1 ~* \' i3 _ - 11、非负浮点数(正浮点数 + 0)
5 V7 c1 g$ U* o0 b - 12、非正浮点数(负浮点数 + 0)
* m& N8 g$ s4 y; \" `0 I - 13、邮箱地址0 j7 `4 y! i2 |
- 14、颜色值匹配0 L* o: m r6 y
- 15、url匹配* q$ _ O7 K6 n0 J
- 16、纯仅中文字符5 c$ }7 T7 _. p/ \, D) V
- 17、仅ACSII字符
6 Y: [+ w. S6 z' L8 B! \. I' z! [( O - 18、邮政编码& L& f- o+ u2 k- H$ @1 H/ W- K
- 19、国内手机号码
# O" m5 m- i# x - 20、IP V4 地址
8 d" e, a/ J9 S5 r' D, J - 21、非空字符# K& m( K) Z V
- 22、图片后缀# Y( i. d# w. W
- 23、音频后缀: g" S0 C* M! D. K) u$ J
- 24、视频后缀
2 L$ H; X7 y8 B- Y - 25、压缩文件后缀4 i) q/ t2 V% e: [! M
- 26、日期格式 + u; A. L0 A9 P9 j
- 27、日期和时间格式
( O& O+ ^8 e" t5 ?: }0 L) y - 28、QQ号码& U; R5 Y. Z2 f: h: P
- 29、电话号码的函数(包括验证国内区号,国际区号,分机号); |' k/ {6 ]0 ~+ z8 N; c( [. W
- 30、用户名注册! ^% l. ?3 @: B( D. J$ t& S
- 31、字母数字组合
, \8 C7 J" P9 Z, A/ ^9 b, ?- p - 32、纯字母6 B: a3 N4 j+ E5 ^- c' c; z$ n
- 33、纯大写字母
) \! s4 p/ f+ H4 m. L - 34、纯小写字母
, Q$ R3 u6 ]& r4 ] - 35、第二代身份证号码匹配9 `2 j! @' Q% J; u' [! u( A
- 36、数字或字母
% Q7 s3 ~& Y }( | 2 m7 u) D& T7 l Z5 @; }. I" }
写在前面
- i/ d! h& b/ r: x5 }$ X& r' x+ `7 `) q/ P) |; Y; [/ Z) r7 k
我们在日常的Java开发中,经常需要处理一些字符串,这个时候正则表达式是非常有用的。几乎在所有的编程语言中都支持正则表达式。以下我将压箱底多年的干货搬出来给大家参考,都是我们日常使用频次比较高的正则表达式,希望能能大大提高你的工作效率。如果本文对大家有帮助,大家可以关注“Tom弹架构”,后续会连载正则表达式的基础知识。0 v; M0 ]0 Q# a/ x; [1 ~/ j4 Z
; I A' \2 u- G. e- P1、整数0 f, y; O. ?' `7 W, D" @
1 \$ F' N$ ]; a
 - 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、正整数
% Q) x; N1 |2 `2 w8 v
' ?$ @6 |. }( Z& Y - 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、负整数7 @' C; C, c( w3 ?8 C# ]5 S& E) @
D& J# x3 T5 v8 U' K* K
 - 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、数字
% o- B. X9 J" h, B: d$ o& a4 Y
( M6 [% q1 D% L2 Q" {7 c - 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)
5 S/ y# Y0 X3 Z+ ?, U: l- 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)
% Z4 U( K# }; b: ^: I9 d9 A+ f! X5 s1 |0 ?
- 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、浮点数1 P0 i" \2 M$ X* Y7 I( i, e t
" u) s) @7 F0 t$ l4 c
- 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、正浮点数
* k5 f- {* ^3 N, h" E" R3 K& `/ J$ X; T/ |& ~. C, L k9 S+ Q
 - 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、负浮点数3 H+ Q2 @, J0 @7 z- [
3 X( |" I4 }! v, q. @1 D3 u
- 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、浮点数' G' o" \' f3 s8 z; r% W' l+ h1 J
. O: A5 ]" `6 v5 H- ^/ L/ A
 - 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)# S: p. \4 E; j* t3 v) d
8 G" e/ A+ ~" `0 r
- 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)9 H! U3 P0 M* b0 W
4 f2 x& w, B, ?9 s0 V
- 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、邮箱地址
! O! r& q9 k& m* k9 _" ^
5 h# {9 C2 S% n3 ^# i- 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、颜色值匹配
! `/ P8 q7 l! v& K5 g; |& _) @3 y2 g6 L7 k2 [
 - 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匹配
5 z. ?, L& Z: [" H7 ^2 r6 V. m2 _2 {0 L9 \: c, J. C# ?- D6 o8 q
- 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、纯仅中文字符. V2 a7 M# _5 M5 I! X
% T b# w2 P5 z1 T - 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字符 n4 n* w8 N" C$ }1 F0 h/ }
- W2 f- K0 [( k - public static final String ascii = "^[\\x00-\\xFF]+$"; //仅ACSII字符 // 正例 System.out.println(Pattern.matches(ascii,"abc123")); // true // 反例 System.out.println(Pattern.matches(ascii,"にそ①②③")); // false
复制代码 18、邮政编码
+ _9 X1 u! Q% b" \2 O- z* J# u0 Q6 v! k# R! H7 K
 - public static final String zipcode = "^\\d{6}$"; //邮编 // 正例 System.out.println(Pattern.matches(zipcode,"100000")); // true // 反例 System.out.println(Pattern.matches(zipcode,"1000000")); // false
复制代码 19、国内手机号码
* A1 \/ c* w3 y9 r1 _6 j! t) d( f; q
* s" Y3 c0 }) L$ ?: q6 d `- 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 地址
# Y7 G& d I5 A8 S6 B: N1 E" O
0 [0 ? A% Y" l) ~* p- 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、非空字符
0 e1 X) v3 O, J$ s3 i) O2 G3 n/ X! B% A4 ?
- public static final String notempty = "^\\S+$"; //非空 // 正例 System.out.println(Pattern.matches(notempty," abc ")); // true // 反例 System.out.println(Pattern.matches(notempty,"")); // false
复制代码 22、图片后缀
) m& O! |# v# E& U5 v* ]) ?9 j, F: {" a7 \1 |1 [( }: p1 L
 - 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、音频后缀6 g+ o7 N6 ]- ~; ?4 O8 ?- A
3 o* w \" j* K; S a- 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、视频后缀
# a6 O$ l* a' q! Y- R) g, K* t
9 | ? z5 T; M- 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、压缩文件后缀
9 A: U' B! s1 e9 u5 }, _% @
- E U3 B$ ^8 e+ N: ~- 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、日期格式
8 ?4 t! t. S& S7 Y* s% m! `
8 \" ~" y8 {- _- B- 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、日期和时间格式8 }. a% l% w! a! C8 V. M( g8 B
3 {8 o+ u) d+ O+ ^% L- 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号码6 F- `5 j6 |- p1 s8 R, Q8 `
( Z' c8 ~# h& q* I1 m
 - 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、电话号码的函数(包括验证国内区号,国际区号,分机号)
" P4 P' h7 [7 M- [- \8 G6 M5 A' V5 N, t7 f3 x
- 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、用户名注册
7 }. v6 D) R' |# f2 V( y, f' W
2 i6 r Q! M' z - 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、字母数字组合
1 g2 q$ k# u, y" c+ n* D- public static final String allstring = "^\\w+$"; //字母数字组合 // 正例 System.out.println(Pattern.matches(allstring,"abc123")); // true // 反例 System.out.println(Pattern.matches(allstring,"abc123%^&")); // false
复制代码 32、纯字母8 a1 r, j+ W9 F5 w8 v$ u
- 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、纯大写字母
2 n1 T( R/ y$ {/ g& M, {) I7 [3 z2 O- 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、纯小写字母4 k3 Z" E+ E1 D" J3 l
- 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、第二代身份证号码匹配
( C% }/ R. R+ _- 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、数字或字母6 U" K/ q" T1 y0 W* b; @
- 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%)的文章就介绍到这了,更多相关正则表达式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
$ v" t. |! D8 |/ z* X( h1 p4 W; ^) j s' h: \
来源:http://www.jb51.net/article/229226.htm8 _; x- X9 Z1 M
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|