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

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 6059|回复: 0

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

[复制链接]

21

主题

0

回帖

10

积分

新手上路

积分
10
发表于 2021-11-30 18:26:07 | 显示全部楼层 |阅读模式 来自 中国
目录4 [* s) L7 X* E: d3 F9 l

) b7 ]" X4 f; i8 S, b4 D( G

    ; H2 y' \1 X6 r5 E
  • 写在前面7 E1 c/ {. ]8 J  k! g6 ^
  • 1、整数6 B2 s: }; Q1 q4 _; o; U& C
  • 2、正整数4 O9 q- s* E& z5 A, x9 x
  • 3、负整数
    ( A( ?3 m4 O3 `6 P9 z8 f( }, G3 _' o+ k
  • 4、数字7 s/ ?0 w8 K5 i: y
  • 5、正数(正整数 + 0)  M. j0 ~* y- X5 E6 I; u
  • 6、负数(负整数 + 0)! p6 p) @0 P4 ?) m
  • 7、浮点数
    . y" ^/ t( M# x/ p- @
  • 8、正浮点数
    0 j. C7 p3 [1 g5 u7 m
  • 9、负浮点数
    8 ~2 f- Z9 _# A" V* J
  • 10、浮点数+ c4 O5 J3 e" b7 r/ E' e8 w
  • 11、非负浮点数(正浮点数 + 0)" k/ T6 w% E# T
  • 12、非正浮点数(负浮点数 + 0)/ ^4 s3 W" Q" S/ X
  • 13、邮箱地址
    $ [4 @* H) k. z/ ?1 h8 I2 {! \# L
  • 14、颜色值匹配
    , R+ F3 T7 T. r, m; [. K
  • 15、url匹配9 f6 b* @; ~1 q: E8 [
  • 16、纯仅中文字符
    3 }' J  y3 P0 F# ~1 I, a; x
  • 17、仅ACSII字符
    , x9 b, R' v  ]- Z/ @, S& D
  • 18、邮政编码
    6 l8 k1 q$ ^" x9 z& K
  • 19、国内手机号码; c+ y5 b% P! c: p* S7 G* p0 g
  • 20、IP V4 地址3 {$ _1 O. M/ ]; D0 C3 Q) ^
  • 21、非空字符
    9 {9 K2 M8 V4 y6 K
  • 22、图片后缀
      ]* E8 h) m6 t9 \5 v) m7 z. q/ ~
  • 23、音频后缀
    4 y9 d. v3 X4 J7 E! `0 o9 B# x' T
  • 24、视频后缀
    8 V+ x: r# K5 S
  • 25、压缩文件后缀
      Q0 [: j; Y1 I. }) N
  • 26、日期格式 0 r) p) O9 n/ z# R
  • 27、日期和时间格式
    7 w: ^+ S4 Z9 w
  • 28、QQ号码
    2 E, a6 D$ F) w& s$ Q4 o% Y3 c
  • 29、电话号码的函数(包括验证国内区号,国际区号,分机号)
    # W5 ~( {2 [1 X  ?6 N
  • 30、用户名注册
    - f4 g4 C% Z$ n. [* B! ^
  • 31、字母数字组合
    : E9 _8 X: f4 W6 Z7 u* j
  • 32、纯字母
    , \- m& S* _5 A2 Q$ u! B& t& K
  • 33、纯大写字母6 T* i; [% v" L/ }  E
  • 34、纯小写字母
      I, [  ?. L3 a* a, o" E- s
  • 35、第二代身份证号码匹配
    1 ]$ @0 L/ y) i' Q2 S  ~0 h
  • 36、数字或字母4 [* C  G; n& r2 D( v6 a$ p
* T. N& a8 x2 V6 f% U" s. m; c
写在前面
  w; V+ W' ?# E' h0 I) _3 c' Q3 h  z& @" U
我们在日常的Java开发中,经常需要处理一些字符串,这个时候正则表达式是非常有用的。几乎在所有的编程语言中都支持正则表达式。以下我将压箱底多年的干货搬出来给大家参考,都是我们日常使用频次比较高的正则表达式,希望能能大大提高你的工作效率。如果本文对大家有帮助,大家可以关注“Tom弹架构”,后续会连载正则表达式的基础知识。7 o4 k. |' I7 }- y8 n% G

& A5 w7 k6 {1 W1、整数7 d+ m3 y. V3 ^4 F6 t4 C% g
# T% h% m' P2 h. ^9 ?! Z' @! M) C
  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、正整数
9 X3 @3 m- v7 o$ e" R) n8 D1 H
; w7 [7 k1 H6 l5 T: S7 D
  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、负整数
* @7 D' |3 ]& ]  f: L2 S, X& p3 @( T0 y  j/ P. l/ \) H
  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、数字5 D7 F# o+ ?$ K- [* v

& T8 W$ q7 ]9 ?9 |& c, i1 \/ M
  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)0 w: c: U: ^) O# J' }) T
  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)
! L* L5 c, t1 w' {$ K
3 B* h$ J. r* O3 O" B7 k% w
  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、浮点数
6 J# `* f6 \# g  Z8 V3 ?5 L0 H' V2 ]% L6 }
  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、正浮点数
# ~8 F" n/ u) R
( j. e& i4 A/ Z3 Y8 F; G# E+ k
  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、负浮点数
; G% V% z6 m4 t' @/ \
/ \3 W) u4 ^! }
  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、浮点数
2 w+ v7 G& q& I7 B0 t& Z
2 n0 v9 Y$ U) e: `7 Q: X  a/ l
  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)1 {# W! _# ]$ \  [) c+ i) p
) m2 \7 w9 ]' p9 D8 {; U
  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)
4 U( K5 }0 ]! ]; d* Y0 i7 M& W
, h1 K% O5 u( a; C3 H% o  x
  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、邮箱地址8 H" n$ l$ d2 I1 M- T3 B

) e( E- \& ^; a/ f+ I' q, y4 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、颜色值匹配
7 `, S* f' J  s3 k. u' @. K: R0 P& ^' d3 Z
  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匹配
2 ~$ B' Q7 Q. f9 H' g0 {( ]4 R5 Z) z0 o! f% \, R+ m7 J; R
  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、纯仅中文字符
( V. E% G+ t2 }; N
% H. t+ Z, c+ u. ^1 L& z: K' d
  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 c" D5 ?9 n
0 O3 ^$ ^- Y8 t* c; ]9 d
  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、邮政编码6 r4 D: G/ g$ F, w6 q, M" U# ~& y
+ f  J! J% j  d5 Y) O" \; b
  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、国内手机号码$ {" m: Z+ w0 j+ Q) d6 u
/ ~& P5 A3 J; F0 r! `8 R7 S0 c, l
  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 地址& n4 Y7 J( J+ b/ \! c6 ?
+ l4 r4 u4 F1 \4 |4 F! e
  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、非空字符% f8 _% L1 [/ I9 H9 `

2 Z5 Q% h1 _4 t1 U
  1. public static final String notempty = "^\\S+$";                     //非空        // 正例     System.out.println(Pattern.matches(notempty,"  abc ")); // true        // 反例     System.out.println(Pattern.matches(notempty,"")); // false
复制代码
22、图片后缀
+ J8 g5 J0 O5 C4 m1 _) x' Q- O: |1 ?: ~' H+ L
  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、音频后缀
0 s* m# Y% t; E8 m9 y6 k& y- N1 D3 y+ c1 h9 O8 B' Y
  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、视频后缀7 j$ ~( x: V% T) S
7 R+ ~6 S: q9 s/ W  }
  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、压缩文件后缀
: ]$ q7 h( @5 ?/ G
$ r# H2 S' {2 f/ W/ D- \" Z
  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、日期格式 0 [4 N* G8 X; B; x- y! w8 \2 g5 `

! j* b6 u* V: M) Z
  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、日期和时间格式
& q9 q- [  I9 y! `, J6 x
  A' V5 _1 m& 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号码
; D3 [1 f% S! M, w2 W
/ }4 p- c4 H* X" h" c/ h' U* @
  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、电话号码的函数(包括验证国内区号,国际区号,分机号)9 [6 o  E2 t4 u" K8 a. U3 x: q
6 T: J2 M# S9 @1 F5 K" G1 u$ W
  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、用户名注册% R" n2 Z- b) A, e  G) {* h) {3 {
- H5 j: W, G) i# ^& q# Q
  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、字母数字组合
" `) `2 X8 k4 P, y$ I9 F! d
  1. public static final String allstring = "^\\w+$"; //字母数字组合        // 正例     System.out.println(Pattern.matches(allstring,"abc123")); // true        // 反例     System.out.println(Pattern.matches(allstring,"abc123%^&")); // false
复制代码
32、纯字母
1 V0 p! _; N# `8 G3 ^7 d1 z
  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、纯大写字母
: J0 z; [3 @  \0 M, F& H" N" S; e
  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、纯小写字母
; M; [8 F  z6 t; A3 J
  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、第二代身份证号码匹配
) o& v) g" T3 I( k- m
  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、数字或字母
, h+ v& @& a6 h; @
  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%)的文章就介绍到这了,更多相关正则表达式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!5 m) A9 F) N+ V: u( P& @* C' l
0 v+ q; ]* Z, m$ o' z4 L0 w. {
来源:http://www.jb51.net/article/229226.htm5 U( e: u+ A/ g5 G
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

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

×

帖子地址: 

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-19 01:36 , Processed in 0.041827 second(s), 25 queries .

Powered by Mxzdjyxk! X3.5

© 2001-2026 Discuz! Team.

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