京东6.18大促主会场领京享红包更优惠

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 6037|回复: 0

36个正则表达式(开发效率提高80%)

[复制链接]

21

主题

0

回帖

10

积分

新手上路

积分
10
发表于 2021-11-30 18:26:07 | 显示全部楼层 |阅读模式 来自 中国
目录7 O: v4 Z% [# L6 W+ B

- q5 R- G) _: Y! Z

    3 q& G2 \; \, J& W* t; u  l
  • 写在前面
    ( j  C% z6 D, ], K& O2 J3 k1 [
  • 1、整数0 X) W" U1 Z  f
  • 2、正整数6 V: l' |2 Y2 w8 v$ _+ r
  • 3、负整数
    2 {; H2 B1 D: U1 J
  • 4、数字
    / t  n" T) F' d! L" ?2 j3 ^$ I
  • 5、正数(正整数 + 0)
    % m0 I: @/ a3 p1 A. a4 l6 Z
  • 6、负数(负整数 + 0)7 m. s, N' ~4 k8 W
  • 7、浮点数0 h% g; F5 z$ `) X
  • 8、正浮点数3 `! z. o2 x$ Q# r
  • 9、负浮点数
    ( j+ c& B9 a2 [& w/ D3 x
  • 10、浮点数2 @1 R2 S$ H' y+ K
  • 11、非负浮点数(正浮点数 + 0)7 a8 E5 u$ k( f0 |
  • 12、非正浮点数(负浮点数 + 0)8 e6 P0 c; u$ v) g
  • 13、邮箱地址! v) I: C* [) |5 V. z9 Z9 s
  • 14、颜色值匹配
    ; ^$ M+ C; i/ X; B! w
  • 15、url匹配
    ! ^1 d/ {9 r7 i- m( G5 O
  • 16、纯仅中文字符9 s; Q9 s$ P' \9 M9 q7 w) H; u
  • 17、仅ACSII字符
    9 Q; e0 {0 [% q3 P! L% s
  • 18、邮政编码
    9 o: m, m6 x6 N6 P" S
  • 19、国内手机号码+ k& V8 g+ p( J$ _
  • 20、IP V4 地址/ q! e. q5 T$ ~- L
  • 21、非空字符
    7 |% S' J& {! P
  • 22、图片后缀/ y5 p( \, D: x# o. t0 H9 a- c
  • 23、音频后缀3 ]. n/ B5 L- H2 A
  • 24、视频后缀
    ( f) U0 [# k* Y( a) W
  • 25、压缩文件后缀2 ~' q2 z" N# a; L: Y' L
  • 26、日期格式 / ]# U% d* }) }, l
  • 27、日期和时间格式  R% u% B  _* m& ^1 M! Q
  • 28、QQ号码
    & D9 b5 g+ r5 c9 B" P/ \
  • 29、电话号码的函数(包括验证国内区号,国际区号,分机号)
    + x3 e1 p. Q, d5 B8 m4 z2 G
  • 30、用户名注册2 X; u- h3 ~- w+ L
  • 31、字母数字组合4 R3 N3 J) b6 C1 M, t
  • 32、纯字母0 F3 S# O6 O, v
  • 33、纯大写字母
    2 Q5 \1 l1 H, {/ A4 e2 X
  • 34、纯小写字母% L- m, X" j. E
  • 35、第二代身份证号码匹配, Z1 U# a1 P7 y  k. ?
  • 36、数字或字母5 e0 |4 e! ?0 N: N2 L3 \: Z& l& a

# r9 E( p5 N: S6 M写在前面
- Z+ H" C- _9 V8 w- w1 g, c
4 k- }; r) B" s# @我们在日常的Java开发中,经常需要处理一些字符串,这个时候正则表达式是非常有用的。几乎在所有的编程语言中都支持正则表达式。以下我将压箱底多年的干货搬出来给大家参考,都是我们日常使用频次比较高的正则表达式,希望能能大大提高你的工作效率。如果本文对大家有帮助,大家可以关注“Tom弹架构”,后续会连载正则表达式的基础知识。* j- f! t% g+ b% n8 ]
; }# e+ E# j8 V* g9 ^" V$ J5 G! q
1、整数; i* `$ o4 N* J/ p9 I

5 d- G; q2 \  P0 j
  1. 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、正整数
' m/ F- @8 |$ f: g8 r+ U8 c% t, V
) S) V4 r8 g4 u4 \- x. j
  1. 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、负整数+ b8 r% e. X4 y( t9 V' c6 `( h2 ]
0 u) ]# h( ?* \5 ?3 R
  1. 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、数字0 d, @: D, X! l* a2 @' H8 K

- E' ?6 k" Z3 a- ~
  1. 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)  L. \. {$ k; `2 }. Q
  1. 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)' j0 ?8 c; l0 R. {& f& V

- a0 Y# M: l2 o4 Z  @5 U- y  i
  1. 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、浮点数8 G' J$ p  n3 `& L

+ D! M! u# B/ ]+ H- i1 n- }: |
  1. 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、正浮点数3 v; l. e2 p# ]& l) E. I- e2 k
! C1 R6 D# Q8 h1 Z
  1. 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、负浮点数9 \! g9 F, m% y
' H& s2 X, ?5 w5 x- }& o# s
  1. 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、浮点数( j6 u0 K6 g# D1 f

3 c1 P" Q, g& n1 O: P
  1. 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)
  n2 v4 [- P$ I! t5 h' Q- q# q
+ `: T- k/ {8 X3 \& C$ {
  1. 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)
# H( q( \. ^' ]; ~# K. x" Y, f7 x+ p1 R, }2 |3 @
  1. 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、邮箱地址
* A6 t# o) ~& L0 \- y
1 F! H# O# f- T; q
  1. 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、颜色值匹配+ J7 E) ]# O6 r5 A
: H( `3 A( Q4 q) v1 V6 d
  1. 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匹配
. h2 R$ z* F1 V0 S! M9 V0 x6 {# z! a# N% {3 x" l0 ]
  1. 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、纯仅中文字符- ~$ E' Z% O, C8 ]1 d
% _( e/ q! y+ }! a5 S6 [
  1. 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字符
1 S) G" x, I4 p6 z
* |2 C3 _0 Z* w* A! h* P& O+ L
  1. public static final String ascii = "^[\\x00-\\xFF]+$";              //仅ACSII字符        // 正例     System.out.println(Pattern.matches(ascii,"abc123")); // true        // 反例     System.out.println(Pattern.matches(ascii,"にそ①②③")); // false
复制代码
18、邮政编码
3 v" C; i3 C, F8 ~% L7 Q; |1 B
, T/ n% ^7 }( X7 C2 h+ K. V
  1. public static final String zipcode = "^\\d{6}$";                        //邮编        // 正例     System.out.println(Pattern.matches(zipcode,"100000")); // true        // 反例     System.out.println(Pattern.matches(zipcode,"1000000")); // false
复制代码
19、国内手机号码
- [) t: o* z6 n! e$ `1 v: ]) E) U6 T
  1. 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 地址
2 Y5 W( n: {% D. q; g2 n  H1 Q  |2 ]8 x9 j
  1. 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、非空字符( l) \& y% V/ n. r) w

7 _# q5 f: d  u9 }3 W
  1. public static final String notempty = "^\\S+$";                     //非空        // 正例     System.out.println(Pattern.matches(notempty,"  abc ")); // true        // 反例     System.out.println(Pattern.matches(notempty,"")); // false
复制代码
22、图片后缀
5 S9 B+ _0 x: `9 V( A8 K8 F5 s( f5 L6 A+ W# u) Y: x6 [
  1. 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、音频后缀
" @& Z  z! ~: Y2 w5 M* T) l! \; {# O8 D3 U2 \- a4 Z% x
  1. 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、视频后缀, T- n  g) Y+ J) w  }
) P0 Y# M8 }: @% C3 `
  1. 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、压缩文件后缀
0 d2 B, L( q2 R' U3 G4 M
0 j1 e" M! \* n" B" ~) v8 v5 |8 D
  1. 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、日期格式 : b: z% a  @8 ~+ j' d

1 g7 K0 n  g0 F
  1. 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、日期和时间格式
. O$ a9 w% `1 B
7 R8 g) [! R) A) c
  1. 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号码0 u' s7 D  X1 y0 c$ z, u

9 S* b5 k) T, l: ^7 l2 \' g
  1. 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、电话号码的函数(包括验证国内区号,国际区号,分机号)
8 ?$ w  p* X+ U. N' }9 F) Y4 A  h; j$ c% U. [* O2 F5 p4 C9 h$ N
  1. 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、用户名注册+ i& F6 Z7 B' k0 D/ g0 n

1 ?% k+ G8 V5 R' n
  1. 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、字母数字组合
% r( u$ _. O  ]4 f8 K. W2 t2 |6 z
  1. public static final String allstring = "^\\w+$"; //字母数字组合        // 正例     System.out.println(Pattern.matches(allstring,"abc123")); // true        // 反例     System.out.println(Pattern.matches(allstring,"abc123%^&")); // false
复制代码
32、纯字母" d1 `0 N/ D7 @; H8 a  ~
  1. 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、纯大写字母
; G/ M, \- n5 S  a
  1. 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、纯小写字母! o' j& `( s& _
  1. 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、第二代身份证号码匹配" y- v) g2 Y* o( {! o
  1. 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、数字或字母
+ T* h+ f. Y; B4 y  i, A3 f
  1. 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%)的文章就介绍到这了,更多相关正则表达式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!/ _4 s5 c) j- `' E+ ?* W8 h
, G+ P4 j+ C0 m3 r% z
来源:http://www.jb51.net/article/229226.htm
) p( U% z* Z( N免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×

帖子地址: 

梦想之都-俊月星空 优酷自频道欢迎您 http://i.youku.com/zhaojun917
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|梦想之都-俊月星空 ( 粤ICP备18056059号 )|网站地图

GMT+8, 2026-7-22 23:28 , Processed in 0.054539 second(s), 25 queries .

Powered by Mxzdjyxk! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表