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

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 6062|回复: 0

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

[复制链接]

21

主题

0

回帖

10

积分

新手上路

积分
10
发表于 2021-11-30 18:26:07 | 显示全部楼层 |阅读模式 来自 中国
目录
9 S! K% {; d6 h; R; ~  E7 S( c3 C; ^
    # e! m; G4 h& e
  • 写在前面
    # g! `( W5 f/ I% s* t! v( W5 {
  • 1、整数
    0 f: U. T$ H' X- Z+ T
  • 2、正整数
    4 s7 N, p  u% D& _
  • 3、负整数
    6 `9 g$ c1 P+ @3 g3 A% K0 _
  • 4、数字1 D$ a" ^: v+ }3 a. i; F
  • 5、正数(正整数 + 0)
    & q! w" ?( t' x
  • 6、负数(负整数 + 0)
    1 u: T) E4 k: q3 l/ K* ]
  • 7、浮点数
    . ^2 m# b2 V# E4 p8 z) {& J% W
  • 8、正浮点数6 N( `* o; l% S
  • 9、负浮点数
    $ ]" X# a! Y. i& K+ m
  • 10、浮点数
    " B( N; G* Z  N2 {: A
  • 11、非负浮点数(正浮点数 + 0)
    # v) F0 E) T; }2 e# _
  • 12、非正浮点数(负浮点数 + 0)
    * F3 _8 b- j# A# X5 B: R
  • 13、邮箱地址- z, }! [; i' _8 b( y, x+ u
  • 14、颜色值匹配
    7 F" _, z5 m0 U/ X; Z; G
  • 15、url匹配* p' w) |; o, C$ a# e1 v6 r0 `
  • 16、纯仅中文字符; H* Q7 P! ^' M  C" L8 t
  • 17、仅ACSII字符6 v; \3 v2 r: r; v/ ]9 v8 ?
  • 18、邮政编码
    % H& g0 V9 w0 ]' l- |
  • 19、国内手机号码
    7 |+ X5 v3 o- K$ d
  • 20、IP V4 地址. _7 B  w! U0 q* D1 E. w
  • 21、非空字符
    , x$ Z9 O9 f; _: d4 {3 t4 R: \* `
  • 22、图片后缀! v3 U' d4 X8 b3 `
  • 23、音频后缀3 e' t1 r; S/ q
  • 24、视频后缀9 Y( d- }5 Z* c  [$ Q& H; f0 c
  • 25、压缩文件后缀+ ^* y8 L3 N4 s2 p
  • 26、日期格式 ! E2 D, w% l- Q- G
  • 27、日期和时间格式
    1 h7 f" S5 D- V4 W
  • 28、QQ号码
    3 a9 Q* K9 m# y8 \% K
  • 29、电话号码的函数(包括验证国内区号,国际区号,分机号)# d  |+ Q/ U; ?4 m( G+ a1 r
  • 30、用户名注册; C9 \# a: z3 g) w) w
  • 31、字母数字组合$ O" h, H2 f  D: a0 `6 A0 @1 g
  • 32、纯字母
    4 a" {, M7 O! ?0 `7 g
  • 33、纯大写字母
    5 J& s  v9 j' \( k' p* \
  • 34、纯小写字母4 R! I6 Z/ f* a* H. T( A
  • 35、第二代身份证号码匹配& w/ \( F$ L" i# G: c0 @- ~
  • 36、数字或字母
    ( _* ?0 @( Y* o
7 L; ~, T5 U% K" {/ Z; F4 d" K
写在前面* e3 }5 |5 ^7 t0 ]' G* S1 e0 B
8 H( b  o! V0 A, |
我们在日常的Java开发中,经常需要处理一些字符串,这个时候正则表达式是非常有用的。几乎在所有的编程语言中都支持正则表达式。以下我将压箱底多年的干货搬出来给大家参考,都是我们日常使用频次比较高的正则表达式,希望能能大大提高你的工作效率。如果本文对大家有帮助,大家可以关注“Tom弹架构”,后续会连载正则表达式的基础知识。
. e+ y0 B. z1 o$ q5 L3 d
$ t' Q4 V( e: x! k1、整数
: E# C5 X/ J. ?5 T0 s1 w/ `0 `
0 v/ i5 e/ O  p$ 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、正整数
9 z% z, [" T3 o' n) _2 L, Q8 l
; a3 F6 A" q4 j7 a, r/ \
  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、负整数4 L8 \6 @+ n4 G1 J  Y

. Z( z3 A, l& u% S
  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、数字
( u% p- r5 _( C' F1 c: L: h& \0 _6 \' P8 L  c$ H$ Z- g
  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)9 n. X% f( x* T# U  G9 F
  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)
; R! H  h0 X7 U3 O# K  P
' u* L, t' X: x# g2 N. ~: z  k
  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、浮点数
1 X7 d8 T" A9 b* m3 P( Y  X
8 _9 f: m. \3 O
  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、正浮点数
# `7 D1 S, F4 d) G) V1 C/ H# \* K: ]4 t& a: s- s0 N6 |# b7 }
  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 n7 R, e, q5 _* q( k2 Y0 [
& M0 P+ {0 i# C8 M" M/ u
  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、浮点数! L+ D$ i  g4 S# @* N  S& H" J

5 f* [. y" I' |, v
  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)$ ~/ C' R# j3 E1 ]5 n3 t- e
+ `+ E8 Z" v; v% F
  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)7 a$ V" w' Y2 E& V" H1 t
+ L! |6 r+ M9 q# S1 E
  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、邮箱地址! S+ t" C$ C" Z4 [. C! G4 T

6 Y6 `% d5 i/ T7 |5 t
  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  s% Y- O* @1 _$ Z' W* d
- f, b$ e- U) ]  |. e3 ~/ A) q
  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匹配" ]' d* S' y4 \/ @: z. F

2 ]0 ~$ J# l: h  q' 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、纯仅中文字符
) ^: N, g9 M! n' \* H2 r5 O4 Y5 i0 a* y! }1 D+ g; K1 k
  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字符- o# y+ y* s! M$ w
8 ?6 j8 @( M  n6 ]! P
  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、邮政编码
- @5 W3 d& _; f" I: l# [, L. S  H! t# A
  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、国内手机号码( e' w: V' c7 s( u$ `* n) A" w

) |9 _! |' ^, H" o1 p; f6 w
  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 地址+ D% g* z( E8 D& o6 P2 }
4 c0 ]: U0 H, q6 j0 r, 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、非空字符$ c0 S* s5 Z5 O  A* P+ K

3 ]* z; Q; V. x# |& V3 `- _
  1. public static final String notempty = "^\\S+$";                     //非空        // 正例     System.out.println(Pattern.matches(notempty,"  abc ")); // true        // 反例     System.out.println(Pattern.matches(notempty,"")); // false
复制代码
22、图片后缀
: u  N* w5 u. S! ^$ h/ I3 h+ E5 Q5 s2 T  H$ e
  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、音频后缀+ @# m9 b: q/ B1 ~9 ]' D
3 e8 p) C" N+ a! U- Y3 ~, 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、视频后缀+ T* F8 n2 v, Q1 Y- @
2 u8 i5 R( m5 M
  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、压缩文件后缀, o# y5 U2 p1 C. D7 l; P6 z
1 O3 g$ w5 Z$ Q# S4 Y
  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、日期格式 
% k  F1 S9 _# X) A; _
* |9 N( A0 O+ w& n+ P% V/ V: T
  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、日期和时间格式
  l$ ~7 [- Z$ r( p' U& m
# ?& u8 F3 u7 j. Z$ H8 h* S
  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号码
& c6 @5 x+ E0 e/ G% I$ f$ I6 }0 Z. Q5 I( d4 Z9 \
  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、电话号码的函数(包括验证国内区号,国际区号,分机号)
- m" ?% @- Q/ `& ?8 Z+ O' j9 f  {! T) O5 V; E) w- U  h3 t" T
  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 g& T* I7 |) P- l* R+ y5 M6 M4 r0 {  p! t5 Z; N0 [3 U$ b  B
  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 q2 m& D0 H/ [& z  A0 f( c
  1. public static final String allstring = "^\\w+$"; //字母数字组合        // 正例     System.out.println(Pattern.matches(allstring,"abc123")); // true        // 反例     System.out.println(Pattern.matches(allstring,"abc123%^&")); // false
复制代码
32、纯字母3 w+ V# H5 t( k5 M! e! Y5 U4 T
  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、纯大写字母
4 C$ A% y1 b& k! I
  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、纯小写字母; ^# b  `5 M. c% Z, f; g0 C" [
  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、第二代身份证号码匹配
0 H: ^6 q6 V$ ~" H" N3 U
  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、数字或字母/ {7 b! x* @8 p* J
  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 Q' j7 _* P( K! N+ H6 a
& V4 f8 r# D- B' @5 c7 K2 L5 z来源:http://www.jb51.net/article/229226.htm& Z; |  _0 |- j$ u5 ?) E
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

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

×

帖子地址: 

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-19 02:17 , Processed in 0.060889 second(s), 24 queries .

Powered by Mxzdjyxk! X3.5

© 2001-2026 Discuz! Team.

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