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

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 5970|回复: 0

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

[复制链接]

21

主题

0

回帖

10

积分

新手上路

积分
10
发表于 2021-11-30 18:26:07 | 显示全部楼层 |阅读模式 来自 中国
目录8 C7 ?  S( h! t0 r# }

# @& b0 H5 h  Q3 o  D
    - Y3 L( `- M$ k  q" D4 l
  • 写在前面+ W. w6 X$ W5 q; z
  • 1、整数* d9 b: o: V3 k- p$ r7 g
  • 2、正整数; D6 i' G$ M$ R) Y- }
  • 3、负整数) R. P: M0 Y$ E7 }3 {7 o0 b' _' _
  • 4、数字, I  J& }) m2 W0 l/ Y6 t
  • 5、正数(正整数 + 0)
    5 R0 a7 j+ }0 I; N! l
  • 6、负数(负整数 + 0)( J, `# g0 `. c, k0 }
  • 7、浮点数; `8 U9 j3 W1 y+ ^( K
  • 8、正浮点数8 f5 [, _1 p. P. K, e+ ]7 a
  • 9、负浮点数
    ; c2 P8 ~! h9 W% X& ]
  • 10、浮点数& Q7 v7 W& j# ~) A
  • 11、非负浮点数(正浮点数 + 0)
    + ?5 @6 `, F" @9 z1 J8 g
  • 12、非正浮点数(负浮点数 + 0)
    6 i3 Y5 a( v# A% [) v6 c. {5 Q7 w
  • 13、邮箱地址
    2 J- \$ ~$ d/ P( e  H" z
  • 14、颜色值匹配+ g$ {# p' P' T) \# ?
  • 15、url匹配
    ; U- Z& |2 L  R1 G# B# i& K) C
  • 16、纯仅中文字符
    " p8 a# M6 F1 ?4 J7 B  c6 ?
  • 17、仅ACSII字符
    8 h# r; F, x9 v$ P
  • 18、邮政编码
    ! ^8 k  C$ T- {0 T
  • 19、国内手机号码9 n! f# B# y; E+ l7 X
  • 20、IP V4 地址, j- U' Y4 a, g
  • 21、非空字符
    ; [+ a' Q# A5 c+ E/ @: j" f
  • 22、图片后缀
    + ?$ H8 L" d" l
  • 23、音频后缀
    ( Y; ?& ^3 e, r) a, Z
  • 24、视频后缀
    9 q! H. W0 p# G+ S% X
  • 25、压缩文件后缀1 C3 w( z( t+ s' }( h1 Y  d8 A
  • 26、日期格式 
    & g/ k( }1 _2 O' E9 @
  • 27、日期和时间格式: }- N9 b5 y5 \* M& f
  • 28、QQ号码! o. e& V7 H6 V- N# D# g( v5 N6 q
  • 29、电话号码的函数(包括验证国内区号,国际区号,分机号)
    ' `6 s' \6 C; _( n! `( t; t
  • 30、用户名注册
      C0 B0 ^7 i2 N! g
  • 31、字母数字组合6 ]5 }, n2 \8 q0 Y' o, a" m6 Q
  • 32、纯字母- D7 W2 W( j) v4 n
  • 33、纯大写字母
    ' A8 G7 l8 n/ t: J2 _
  • 34、纯小写字母: r3 s$ G; Y) f  B' e7 P. ~+ X1 q- r
  • 35、第二代身份证号码匹配. I" _% a: d; W# s9 [
  • 36、数字或字母
    5 A* `2 H0 l2 p  U. l! a; [" \
2 q% l$ ?9 ^7 H/ C+ q
写在前面# Z, V2 B* k  M3 ?% l# Y
4 e. O" J6 h# }7 U& ~) I
我们在日常的Java开发中,经常需要处理一些字符串,这个时候正则表达式是非常有用的。几乎在所有的编程语言中都支持正则表达式。以下我将压箱底多年的干货搬出来给大家参考,都是我们日常使用频次比较高的正则表达式,希望能能大大提高你的工作效率。如果本文对大家有帮助,大家可以关注“Tom弹架构”,后续会连载正则表达式的基础知识。; C; ^" q. ^- R  M
( B' W4 W+ u4 e; H
1、整数+ f. Z4 l5 o4 [9 ]4 s
/ R+ ^5 g$ {" S) k6 W, I
  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、正整数! g. S& X$ w9 u9 C3 Q
/ B5 G& w- g# k* B, o" P6 Q8 [- h
  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、负整数, q+ p/ _9 v$ H7 s' A

+ F% o) B/ ]+ U, B5 _
  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、数字
3 w" C' ~- R' F8 i! s" p& k$ d" o0 N/ P# D0 t# ^; z
  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)
- n. j: q- j( g0 j4 ]) T4 M
  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)9 ~6 T" x7 [0 e2 K  U0 Q3 p5 q2 ?
. X6 W8 q3 R$ A5 m$ z/ B8 P
  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 S: r4 {, A2 D* f, ]4 B4 U+ {
% S. Z; Z2 `' [
  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、正浮点数! [0 d) t7 B$ O! ]& x; u& ?4 e
  N5 O* O( g6 \
  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、负浮点数1 {+ R% j2 r) F" e! n

' x2 U# d. ^9 e: c6 b: @' X
  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、浮点数
6 }* X1 r% m4 A$ v- {  N. S0 f' {
  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)+ _3 Q( a- _  n  T2 N
- I6 U+ t; Z6 Y4 T; e& O
  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)
* L5 d3 E( L- D9 Y5 g" }/ K
9 I3 L( }( ]# A
  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、邮箱地址5 ~. B' G1 S9 X6 r* Q1 e

& s) A$ P6 H1 v! U/ v. @  \
  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、颜色值匹配
) N) J! M7 e# z0 U9 A9 I, v! p8 C$ _+ p& u5 _: k- ?4 n
  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匹配. j2 q, z2 B6 H5 D
0 c7 {; U7 P7 I" y4 I) i! }
  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、纯仅中文字符
; C( n" j! u% @$ ]
. b% w  z+ t4 e+ s* H/ l
  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字符# U+ K- G  G* ]- J  V  L: B# y
/ s/ C# y! F5 S  R  \2 t
  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、邮政编码! ?- K5 o8 H! g
4 q2 F8 |% v. ?2 c, J. I2 A( F8 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、国内手机号码3 f1 B3 `4 \0 V9 H( A
6 v" q. T- m2 o' p% K1 A
  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 地址* _! e. |+ z' M1 k

  K7 W: c$ Y3 T5 b5 }
  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、非空字符
2 W+ {/ r2 M2 N5 G
/ x, e  W" H% w7 z: ?
  1. public static final String notempty = "^\\S+$";                     //非空        // 正例     System.out.println(Pattern.matches(notempty,"  abc ")); // true        // 反例     System.out.println(Pattern.matches(notempty,"")); // false
复制代码
22、图片后缀: s4 }7 R  y6 x/ D5 p# ~

/ T$ y$ J0 s% M  C; F" ~
  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、音频后缀5 I% ]3 _! N5 U& ]  x

+ \5 C+ x4 r& h4 T3 Q% E
  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、视频后缀
5 R' ]% {! i: w0 N: b1 L  o3 p- H) d& x# k
  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、压缩文件后缀. v) V4 A2 ^! p* }

0 R: R  m" R% B' f6 K2 d1 p
  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、日期格式 5 b: u$ K% X! N: T
6 ]% g1 S' y% 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、日期和时间格式
- ]+ D7 O) n2 K7 T7 u- Y( I
2 T" W$ s6 @( E0 ]
  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号码. H3 u: V5 R6 D, F" h6 x% B4 R8 _

2 Y* v+ i5 V% |( A2 K: i9 h) f& _* ~; V3 }/ f
  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、电话号码的函数(包括验证国内区号,国际区号,分机号)
1 T& O5 \' l) D" S; a7 X& q5 O6 j* o
  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、用户名注册
5 _0 a( l2 I" d5 |
  N) R) m6 B3 |$ M: @% }
  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、字母数字组合
$ A+ ]  K, \  `9 c4 `9 G! |6 w1 ]
  1. public static final String allstring = "^\\w+$"; //字母数字组合        // 正例     System.out.println(Pattern.matches(allstring,"abc123")); // true        // 反例     System.out.println(Pattern.matches(allstring,"abc123%^&")); // false
复制代码
32、纯字母# m3 L, S  D+ ?2 k1 j' a& j
  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、纯大写字母
; a  G/ q. \# W. t& f
  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、纯小写字母
) N8 i6 S2 C+ 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、第二代身份证号码匹配
! K: R6 p2 b' V3 f* b, H, u* [7 [
  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、数字或字母  A# M' n. f' N. S8 ^) k" T
  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%)的文章就介绍到这了,更多相关正则表达式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
" c$ G3 H1 G6 o  r& Y' ^
5 C8 ]4 n1 F( o. ?9 M/ k$ K- \来源:http://www.jb51.net/article/229226.htm% ~6 P; s9 \+ [1 V. M  k
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

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

×

帖子地址: 

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-11 02:08 , Processed in 0.044810 second(s), 23 queries .

Powered by Mxzdjyxk! X3.5

© 2001-2025 Discuz! Team.

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