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

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 6066|回复: 0

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

[复制链接]

21

主题

0

回帖

10

积分

新手上路

积分
10
发表于 2021-11-30 18:26:07 | 显示全部楼层 |阅读模式 来自 中国
目录
  @! i4 l2 e% ]
2 {7 i0 p- o+ Z3 q6 _8 R% K) Z& n
    " _. U3 g- I4 r3 s
  • 写在前面
    $ e9 {8 W* z5 m5 u: h
  • 1、整数' W+ i' W; s9 y' a
  • 2、正整数$ l& a5 z/ x! [, u' @! w4 X
  • 3、负整数
    & o" B' [' G) v, ~) z* j+ b, {$ }( _
  • 4、数字3 Y/ p7 Z- L! u& e5 }- o5 l8 f
  • 5、正数(正整数 + 0): B4 ?9 \! A7 c# v" ?0 _
  • 6、负数(负整数 + 0)' S) K5 q% K; n( L4 _. ]
  • 7、浮点数
    6 K6 I$ d, I; d- W- a
  • 8、正浮点数' Y/ k' a7 Z4 |6 y
  • 9、负浮点数: W8 V4 g6 w# N) o5 D, t
  • 10、浮点数
    - z& G: O+ B  Y- ^% E4 h
  • 11、非负浮点数(正浮点数 + 0)7 v  V1 d1 }7 j( V9 \; y% V) g
  • 12、非正浮点数(负浮点数 + 0)
    5 E' F# k) t" I2 B5 [1 n
  • 13、邮箱地址1 G# U2 V. D* |' ^2 k3 L1 y' J
  • 14、颜色值匹配3 s$ u1 D) F7 S0 p' S
  • 15、url匹配
    6 ]/ w5 r& \! |3 D
  • 16、纯仅中文字符
    4 ]9 @9 k2 s5 T/ K1 ^$ B# e3 V
  • 17、仅ACSII字符
    ( y$ G* h. f& t4 }" U
  • 18、邮政编码  m- y+ V, ~1 Z, C1 N
  • 19、国内手机号码
    9 v2 h) ~) |0 |; O: E0 R
  • 20、IP V4 地址3 K5 m9 b  ?; m2 k
  • 21、非空字符/ v# o1 R- Y, w) l" V
  • 22、图片后缀
    # Q! R0 l0 u) \: A& m$ j
  • 23、音频后缀+ A1 F, e: U( o; v. H7 x
  • 24、视频后缀! Y; A4 O: T8 @0 d4 e* O
  • 25、压缩文件后缀( V% K$ n; _; k) }; k" \
  • 26、日期格式 
    : J  b  l9 F% d' S# c0 f9 E
  • 27、日期和时间格式
      w1 d6 @, y1 y; y; M% V
  • 28、QQ号码! \9 O0 W' g) L  K. @
  • 29、电话号码的函数(包括验证国内区号,国际区号,分机号)
    9 Q5 o6 }* B- \# b$ E1 p& I* w7 M
  • 30、用户名注册- p" Z2 N. \( P  W. Y" q
  • 31、字母数字组合
    ; ?5 L7 l6 @& _, n2 C6 S2 N& ~; f
  • 32、纯字母- {; Y/ Y" K9 O  w- K# x; b: _
  • 33、纯大写字母
      d& E: A0 g' j- m" m
  • 34、纯小写字母0 h3 e9 z$ \9 P& z4 a
  • 35、第二代身份证号码匹配. Y1 t) M. [- o
  • 36、数字或字母* V) y9 H6 }7 u# x  h
* `  s: r" X6 |8 }
写在前面: k  y3 {2 X6 L5 {1 k+ V

* K# r3 E; l/ D我们在日常的Java开发中,经常需要处理一些字符串,这个时候正则表达式是非常有用的。几乎在所有的编程语言中都支持正则表达式。以下我将压箱底多年的干货搬出来给大家参考,都是我们日常使用频次比较高的正则表达式,希望能能大大提高你的工作效率。如果本文对大家有帮助,大家可以关注“Tom弹架构”,后续会连载正则表达式的基础知识。5 ^/ i6 b6 m$ P& ~
, I  G. u8 i0 V9 L" [* R* J7 B& J
1、整数
- e* \* f$ t' U3 v( Q" r' j" L( H5 ]1 f3 U0 p
  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$ ?4 n& h: j0 X0 `  L0 I" M( l$ l8 ~6 i0 i
  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、负整数8 {/ B. I! B' B% [
- ^  n/ H2 u* {+ k0 ?' L) f+ t
  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、数字7 {+ M% R" b) n  k
* I  X4 g3 ^+ y: p
  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)6 {3 G2 S7 W' J
  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)7 s  V( |% u! i0 C
3 e, m4 t7 }( r( G% E; ]* c
  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、浮点数/ F' S9 l2 |8 O0 I1 f+ E
: ^5 |2 W, s' p- b( m- L
  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、正浮点数
& q* j1 e- u  U( `. Z- e! _2 v& k$ [+ c9 f6 z' N9 n
  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、负浮点数/ b8 x! z: f2 _' c$ C* {& I' Q

; T' d; x. x, Y0 P2 \' e' F. T
  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、浮点数$ k9 G4 h2 ?: d3 i2 J; i( Y

" F0 N6 s  h: C* J
  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)
5 P% U  [$ ^5 Q! ~6 E/ T1 k7 j+ t- N2 T
  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)3 C8 @' [0 P  q( |- U

9 X6 w2 |! p  a$ Q; b& Y
  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、邮箱地址1 r% X3 @2 N3 C$ {5 [
7 w0 R5 G' _( W. f& Y8 y4 s% k
  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、颜色值匹配2 ]( W8 `+ J7 p2 D5 i
8 a; ]) M. _' ?
  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匹配
# ?3 c, e4 J3 w" E# U% m
% \. v: u; ^/ f, 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、纯仅中文字符
/ h  w* P4 ^$ x9 h; P2 h" _. l4 G. ?. P" m4 @
  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字符) h) C6 Q- r* q! \2 K- Q
7 E  X; V" R5 X; F( w$ V- ?% J/ c& i
  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、邮政编码, D# f  S4 ^! k
+ A" l) u- p1 ^( L, p0 B7 Y0 d
  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、国内手机号码5 C' k9 }% f/ p, h4 H. j9 E# ~
$ `4 U7 u7 \- N% i
  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 地址
. ?# q2 e4 y' S% b: {+ K
5 d/ _1 |" K$ [3 d/ x
  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、非空字符
& Y4 r$ n) |, Q0 @. P' w% y: {  O
1 J; N/ |" }, J1 b5 T0 T& K
  1. public static final String notempty = "^\\S+$";                     //非空        // 正例     System.out.println(Pattern.matches(notempty,"  abc ")); // true        // 反例     System.out.println(Pattern.matches(notempty,"")); // false
复制代码
22、图片后缀
* V& A* i( _- M# |  m- r0 a; g7 G7 j3 ~) d, T$ |5 E& W( k
  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、音频后缀7 q1 [  W- \, ^' R# n, n" S4 X
6 R' u( \5 {( {- k
  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、视频后缀2 F0 t$ T; z3 h

& b' J8 ?; M! K2 t
  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、压缩文件后缀
2 y' W/ f7 w9 x& o0 w( O; W9 s" G# a( J$ Y6 k$ f" h
  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、日期格式 
1 Y# U$ W4 C) }
- z/ `' R" j, t( Z9 `$ v$ N( `1 p4 \
  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、日期和时间格式1 B6 s2 q% e' Y" _2 |! w; n
- w0 S4 R0 s( w& ]5 j& 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号码
' Z+ D* X! o! O4 `$ `$ c, C; O6 }# O& f# Z0 _- 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、电话号码的函数(包括验证国内区号,国际区号,分机号)' j7 a. _$ X$ w: S4 j: Q
# I5 |# `5 P  P/ g) 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、用户名注册7 R9 j& q- P4 c' o. ]1 G2 V
: H' [6 E" B2 P: a
  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、字母数字组合9 S6 Y+ |2 n# y: j  g" F0 a
  1. public static final String allstring = "^\\w+$"; //字母数字组合        // 正例     System.out.println(Pattern.matches(allstring,"abc123")); // true        // 反例     System.out.println(Pattern.matches(allstring,"abc123%^&")); // false
复制代码
32、纯字母+ K# f: t  n5 i6 `( Y1 r
  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、纯大写字母
$ M" z4 E/ T$ j5 @4 P
  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、纯小写字母
  D1 q# \+ o, R: ~
  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、第二代身份证号码匹配
  {# ~& c1 j  H  b
  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、数字或字母; N3 r5 u& n% c3 ?' r
  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%)的文章就介绍到这了,更多相关正则表达式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
7 N; C# J2 F* g
- `/ a% W2 B+ V6 r1 E来源:http://www.jb51.net/article/229226.htm# R2 ?0 H  b! ~. p4 x* h
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

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

×

帖子地址: 

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-10-21 09:19 , Processed in 0.041355 second(s), 25 queries .

Powered by Mxzdjyxk! X3.5

© 2001-2026 Discuz! Team.

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