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

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 6038|回复: 0

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

[复制链接]

21

主题

0

回帖

10

积分

新手上路

积分
10
发表于 2021-11-30 18:26:07 | 显示全部楼层 |阅读模式 来自 中国
目录. h) J- u( n, W8 t, `0 b) o
. H) ~' R- T/ Z" ]2 r& ?
    ) ^/ O$ r' Z8 }6 C
  • 写在前面$ j% P% E+ c$ W) `. L1 x( d
  • 1、整数3 [  k5 j/ m0 g( {) |; P: u
  • 2、正整数, u' U8 v. w4 }1 N. S7 U6 [
  • 3、负整数
    , S2 ~4 O" Y4 ]4 V0 q3 A3 S
  • 4、数字  X0 o5 Y! ?/ Z' g
  • 5、正数(正整数 + 0)! g' x# |. ~3 ]  E9 i
  • 6、负数(负整数 + 0); V( _: ^6 p! \3 N) k
  • 7、浮点数) `. O! ?+ A$ r6 m9 ]
  • 8、正浮点数! r& ?5 g- }3 ~' F2 i
  • 9、负浮点数) u. d3 o' j; E+ s/ i8 b% V0 L
  • 10、浮点数
    % ?; Q3 b" g. G8 n5 C+ V
  • 11、非负浮点数(正浮点数 + 0)
    & S* H" L/ N- y0 \0 u* Y  \
  • 12、非正浮点数(负浮点数 + 0)' V% k) }, O" {$ d. ^3 Y
  • 13、邮箱地址
    ; \  U1 @8 c1 U# x# Q/ J
  • 14、颜色值匹配
    . C3 h7 }/ O% _1 u: C
  • 15、url匹配
    & `2 ^( M" K2 M  V% f
  • 16、纯仅中文字符
    - V" {  g2 k  w+ `- v
  • 17、仅ACSII字符
    % ~( U0 U, }, c: ^6 T
  • 18、邮政编码5 g; |5 [- E% L! Y  o
  • 19、国内手机号码
    & h/ f+ R* A' i6 p9 e
  • 20、IP V4 地址
    / c; K7 C# r/ b3 Q8 k
  • 21、非空字符
      D2 f8 I3 N: v$ w0 o& a
  • 22、图片后缀
    $ Q. q( x  Y' m; f9 k
  • 23、音频后缀
      q7 R$ I+ @. O' X- n+ J$ k% K
  • 24、视频后缀
    / X* e0 p6 t0 ]5 s$ n
  • 25、压缩文件后缀
    % q, f5 q2 n/ y  C& o
  • 26、日期格式 
    6 ], s4 V; d/ y5 z: W
  • 27、日期和时间格式- l7 n/ @4 |( z% G8 U
  • 28、QQ号码+ I% u5 ?: V' w/ L
  • 29、电话号码的函数(包括验证国内区号,国际区号,分机号)' z# M9 ~: z& J+ E/ Z5 Q3 F
  • 30、用户名注册7 {' p# [3 ?% S( b# l# ?, R  `- ^( T! X
  • 31、字母数字组合
    : ^9 Z' O2 G# n6 @
  • 32、纯字母
    # Y# S, ?% w& c
  • 33、纯大写字母8 G: a7 W$ o# z" Q
  • 34、纯小写字母
    / `! l% w4 s( r* ^8 r! }
  • 35、第二代身份证号码匹配8 x* y& W' P+ n
  • 36、数字或字母  M8 H7 k7 N7 v- Q, ?
3 G; H! Z, v  R) @; y; ?
写在前面' u( |$ g. x' P
& S$ n0 q9 ?( a' w  R
我们在日常的Java开发中,经常需要处理一些字符串,这个时候正则表达式是非常有用的。几乎在所有的编程语言中都支持正则表达式。以下我将压箱底多年的干货搬出来给大家参考,都是我们日常使用频次比较高的正则表达式,希望能能大大提高你的工作效率。如果本文对大家有帮助,大家可以关注“Tom弹架构”,后续会连载正则表达式的基础知识。
' g2 U) {: L1 Z! L  e" S! N6 h- N, G
1、整数& |. {, }2 n) \

. Q: [9 l$ y& h1 s- Q1 U5 H, g
  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、正整数0 \, {: o: _3 w7 ~
7 ^  B" ^" B/ |' |, e/ T2 |& \
  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 v- c# Y) s" [2 f- c) `) N

1 L1 T& `4 @& Q3 [1 p+ ^4 a+ _, [
  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、数字9 n3 Y9 x: R3 J, Y

* X! z5 B: m6 V$ T' ?: h
  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)
2 u7 r- P0 X6 @; Q0 a
  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): m) A4 J6 g% ?% o/ K, v: f" n! T1 E; g

' Y  n- v+ c- P: b( l! \
  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 M: a1 ~" W; |# O$ G7 E
+ j$ H2 l/ I; j9 Y. {
  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、正浮点数
* \. W* w+ i) C! i  {  n) h; @/ J1 ]% T  I4 v; `7 U% d
  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、负浮点数5 F" @! C) p; Y8 M- Z
4 s2 k3 s" k) h  e# y' ^3 y
  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、浮点数4 A/ k9 {9 u* t
* D2 f7 S0 b1 s3 W: S9 L6 ~) |
  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)
+ E  o9 w1 ^, d; q+ f( ~$ o& y2 i8 t+ s
  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): _, O! B6 ?6 ]

2 `% v. r9 i- l) k- 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、邮箱地址
/ C" d, t4 {6 W) z+ ?2 C; W2 A5 {; L: l' G% I; K" N, }* C
  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、颜色值匹配. D; m3 `8 G1 J

' y8 J6 a. w! `
  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匹配
+ f+ T4 c) l& @7 h
, @3 c1 C# h0 C5 s! f+ |
  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、纯仅中文字符
& o/ Y( M$ N5 I* T8 X$ n9 W
' o1 d1 h7 a1 q, V- T
  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字符, p, p- J2 f9 |. O7 s0 E& N/ {

/ G+ c9 A, M) W' z- |# L( B. V
  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 O0 @( G4 p6 @9 A) W) X1 a. U
, W6 _  X: K( G) W" \' K* i+ M
  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、国内手机号码! K! v6 ]2 I  R8 M: F6 i
. ^! @) m; Z( @" G6 k  n8 m
  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 地址
* J* ?9 [5 H6 j9 a( X4 i; G& _  o) k! {& L' \
  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、非空字符
# `9 \  A. T+ K) d
( I3 s0 O  s5 Y' Z# I1 z
  1. public static final String notempty = "^\\S+$";                     //非空        // 正例     System.out.println(Pattern.matches(notempty,"  abc ")); // true        // 反例     System.out.println(Pattern.matches(notempty,"")); // false
复制代码
22、图片后缀; l- u  b+ H" n: x4 i, l# o5 |9 k
5 X  Y% v2 U1 T/ K1 x
  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、音频后缀, `# I' A% T3 l& [; [% J+ n- ^' b
+ U& c; K1 G: H2 J% C
  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 f+ h2 h' P6 Y9 c. Q
" b9 t) e) S7 u: T1 d4 S
  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、压缩文件后缀
' D- Z8 ]! K8 {
5 E9 S/ c9 l, e) W. G
  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、日期格式 9 o0 O# j) g: w& g0 O5 P

5 {. C  V1 Z% L
  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、日期和时间格式
2 v# D0 a2 D/ R1 Q. j* B
) [% S& W/ l' A: G+ k. R# F, Q
  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号码( t& [# x9 i4 R! a
0 V! y7 [& m9 a" H* d0 I
  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 p' J$ U1 Q5 y! W8 W: R' F+ v# g) a" N$ e3 u$ W  s% h
  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、用户名注册
( K' G* s/ ~9 P6 v5 c
. s' A2 U( _; E& j  T3 R4 T
  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、字母数字组合! Y/ o- D. G& Z/ Q$ o2 n
  1. public static final String allstring = "^\\w+$"; //字母数字组合        // 正例     System.out.println(Pattern.matches(allstring,"abc123")); // true        // 反例     System.out.println(Pattern.matches(allstring,"abc123%^&")); // false
复制代码
32、纯字母
% o5 m7 T5 [  N. C7 j$ X& l1 V3 u
  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、纯大写字母
# T9 }% G7 j. J. p- [3 f3 j
  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、纯小写字母
% s$ D* Y- f& f& z
  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、第二代身份证号码匹配5 @! {( y/ r9 C
  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、数字或字母
8 p' ~- s$ k, X  P- A
  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 F/ `0 X- e' d4 u0 ^/ j, \1 ?5 _) O3 e( Y
来源:http://www.jb51.net/article/229226.htm
6 ?$ o) d6 N3 U8 Y4 b免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

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

×

帖子地址: 

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

使用道具 举报

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

本版积分规则

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

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

Powered by Mxzdjyxk! X3.5

© 2001-2026 Discuz! Team.

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