惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Hacker News: Ask HN
Hacker News: Ask HN
N
News and Events Feed by Topic
Forbes - Security
Forbes - Security
The Last Watchdog
The Last Watchdog
TaoSecurity Blog
TaoSecurity Blog
Schneier on Security
Schneier on Security
SecWiki News
SecWiki News
V
Vulnerabilities – Threatpost
Project Zero
Project Zero
O
OpenAI News
W
WeLiveSecurity
Security Archives - TechRepublic
Security Archives - TechRepublic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
H
Hacker News: Front Page
Cisco Talos Blog
Cisco Talos Blog
Spread Privacy
Spread Privacy
Help Net Security
Help Net Security
P
Privacy & Cybersecurity Law Blog
K
Kaspersky official blog
S
Security @ Cisco Blogs
Latest news
Latest news
AWS News Blog
AWS News Blog
U
Unit 42
Martin Fowler
Martin Fowler
阮一峰的网络日志
阮一峰的网络日志
S
Secure Thoughts
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Know Your Adversary
Know Your Adversary
Scott Helme
Scott Helme
博客园 - 司徒正美
B
Blog RSS Feed
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
Docker
Google Online Security Blog
Google Online Security Blog
Jina AI
Jina AI
aimingoo的专栏
aimingoo的专栏
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Last Week in AI
Last Week in AI
月光博客
月光博客
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
SegmentFault 最新的问题
NISL@THU
NISL@THU
T
The Blog of Author Tim Ferriss
C
Cisco Blogs
Attack and Defense Labs
Attack and Defense Labs
小众软件
小众软件

qubeijun的博客

言语理解与表达 父亲往事-我儿子叫冬冬 公务员行测答题顺序及用时及答题技巧 判断推理 数量关系 冬甜夫妇5-10-5-21日本蜜月行程安排回归总结篇(无图片) 冬甜夫妇5.10-5.21日本蜜月行程安排计划篇 5.1 小贝壳疫苗计划 100DAYS 父亲往事-猴牤大爷的“帽子” 父亲往事-姨妈家的葡萄架 父亲往事-十五的月饼 父亲往事-姑妈的压岁钱 父亲往事-母亲的缝纫机父亲的锯 父亲往事-家属来队 父亲往事-新兵连的生活 父亲往事-村北的槐树林 父亲往事-小贝壳 公务员面试 系统架构设计师 图形推理 数学运算 父亲往事-惊蛰 资料分析 父亲往事-偷吃西红柿有绝窍 父亲往事-毯子衣服 父亲往事-水壶煮鸡蛋 父亲往事-煎饼蘸香油 系统架构设计师论文 软件设计师 父亲往事-大哥 资料分析-旧 基于grafana的监控部署 mysql数据库定时备份&运行日志定时清理 各种问题解决答案汇总 Alibaba Cloud Toolkit的使用说明 Dojo简单入门 Angular简单入门 vue基础知识点总结 java处理时间和日期api java常用的io,输入输出 java常见集合汇总 java调试代码不再用system.out.println(),用日志来调试程序 java常用工具类总结 java常用字符串操作总结 python之pymysql增删改查mysql数据库 词性标注集 python获取当前文件夹下的所有的文件名 mysql数据库操作总结 (翻译)Understanding LSTM Networks (翻译)Understanding Convolutional Neural Networks for NLP (翻译)传统和深度学习模型在文本分类中的应用综述与基准 小木虫论坛-学术科研互动平台爬虫 solr命令(持续更新) python json读写操作 python pip安装包镜像 python数字进度条 抓取维基百科分类索引下某一分类下的子分类及其分类下的页面(仅限词条) python3循环读取excel文件并写入json python3 requests动态网页post提交数据 python+selenium搜狗微信主页抓取 2017年5月25日 2017年5月18日 2017年5月17日 2017年5月15日 2017年5月16日 2017年5月14日 2017年5月13日 2017年5月12日 2017年5月11日 2017年5月10日 2017年5月9日 2017年5月8日 2017年5月7日 2017年5月6日 2017年5月5日 2017年5月4日 2017年5月3日 2017年5月2日 2017年5月1日 儿子的信-2013年7月9日 儿子的信-2013年7月8日 儿子的信-2011年11月26日 儿子的信-2010年8月16日 儿子的信-2010年1月2日
java正则表达式收集
qubeijun · 2020-03-02 · via qubeijun的博客

发表于|更新于|java

|总字数:1.4k|阅读时长:7分钟|浏览量:|

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package _11zhengze;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

static boolean isValidMobileNumber(String s) {
return s.matches("\\d{11}");
}
public static void main(String[] args) {
//判断是否是电话号码
System.out.println(isValidMobileNumber("18640274769"));

//判断是否是20##年
String regex = "20\\d\\d";
System.out.println("2019".matches(regex)); // true
System.out.println("2100".matches(regex)); // false

//精确匹配 match
String re1 = "abc";
System.out.println("abc".matches(re1));
System.out.println("Abc".matches(re1));
System.out.println("abcd".matches(re1));

String re2 = "a\\&c"; // 对应的正则是a\&c
System.out.println("a&c".matches(re2));
System.out.println("a-c".matches(re2));
System.out.println("a&&c".matches(re2));

//匹配任意字符
String re3 = "a.c";
System.out.println("abc".matches(re3));
System.out.println("aBc".matches(re3));

//匹配数字
String re4 = "00\\d";
System.out.println("007".matches(re4));
System.out.println("00A".matches(re4));

//匹配常用字符 不能匹配#和空格
String re5 = "java\\w";
System.out.println("javac".matches(re5));
System.out.println("java9".matches(re5));
System.out.println("java_".matches(re5));
System.out.println("java#".matches(re5));
System.out.println("java ".matches(re5));

//匹配空格字符 包括空格和tab
String re6 = "a\\sc";
System.out.println("a c".matches(re6));
System.out.println("a\tc".matches(re6));

//匹配非数字 类似 \W \S
String re7 = "java\\d"; // 对应的正则是java\d
System.out.println("java9".matches(re7));
System.out.println("java10".matches(re7));
System.out.println("javac".matches(re7));

String re8 = "java\\D";
System.out.println("javax".matches(re8));
System.out.println("java#".matches(re8));
System.out.println("java5".matches(re8));

//重复匹配
String re9 = "A\\d*"; //匹配任意字符,包括0个
System.out.println("A".matches(re9));
System.out.println("A380".matches(re9));

String re10 = "A\\d+"; //匹配至少一个字符
System.out.println("A".matches(re10));
System.out.println("A380".matches(re10));

String re11 = "A\\d?"; //匹配0个或一个
System.out.println("A".matches(re11));
System.out.println("A3".matches(re11));
System.out.println("A380".matches(re11));

//匹配指定个数字符
String re12 = "A\\d{3}"; //3个
System.out.println("A380".matches(re12));
String re13 = "A\\d{3,5}"; //3到5个
System.out.println("A380".matches(re13));
System.out.println("A3801".matches(re13));
String re14 = "A\\d{3,}"; //至少三个
System.out.println("A38023421".matches(re14));

/**复杂匹配规则*/
//^匹配开头和$结尾
String re15 = "^A\\d{3}$"; //至少三个
System.out.println("A380".matches(re15));

//匹配指定范围
String re16 = "[123456789]\\d{6,7}";
String re17 = "[1...9]\\d{6,7}";
System.out.println("1864027".matches(re16));
System.out.println("1864027".matches(re17));
String re18 = "[0-9a-fA-F]{6}"; //匹配0-9,a-f,A-F
System.out.println("1A2b3c".matches(re18));
String re19 = "[^1-9]{3}"; //排除法,不包含1-9
System.out.println("ABC".matches(re19));
System.out.println("AB1".matches(re19));

//或规则匹配
String re20 = "java|php";
System.out.println("java".matches(re20));
System.out.println("php".matches(re20));
System.out.println("go".matches(re20));


//使用括号 重复部分
String re21 = "learn\\s(java|php|go)";
System.out.println("learn java".matches(re21));
System.out.println("learn Java".matches(re21));
System.out.println("learn php".matches(re21));
System.out.println("learn Go".matches(re21));

//分组匹配
Pattern pattern = Pattern.compile("(\\d{3,4})\\-(\\d{7,8})");
pattern.matcher("010-12345678").matches(); // true
pattern.matcher("021-123456").matches(); // true
pattern.matcher("022#1234567").matches(); // false
// 获得Matcher对象:
Matcher matcher = pattern.matcher("010-12345678");
if (matcher.matches()) {
String whole = matcher.group(0); // "010-12345678", 0表示匹配的整个字符串
String area = matcher.group(1); // "010", 1表示匹配的第1个子串
String tel = matcher.group(2); // "12345678", 2表示匹配的第2个子串
System.out.println(whole);
System.out.println(area);
System.out.println(tel);
}

//非贪婪匹配 尽可能少的匹配
Pattern pattern1 = Pattern.compile("(\\d+?)(0*)");
Matcher matcher1 = pattern1.matcher("1230000");
if (matcher1.matches()) {
System.out.println("group1=" + matcher1.group(1)); // "123"
System.out.println("group2=" + matcher1.group(2)); // "0000"
}

//分割字符串
String[] s = "a, b ;; c".split("[\\,\\;\\s]+");
System.out.println(s[0]+s[1]+s[2]);

//搜索字符串
String s1 = "the quick brown fox jumps over the lazy dog.";
Pattern p = Pattern.compile("\\wo\\w");
Matcher m = p.matcher(s1);
while (m.find()) {
String sub = s1.substring(m.start(), m.end());
System.out.println(sub);
}

//替换字符串
String s2 = "The quick\t\t brown fox jumps over the lazy dog.";
String r = s2.replaceAll("\\s+", " ");
System.out.println(r); // "The quick brown fox jumps over the lazy dog."

//反向引用
String s3 = "the quick brown fox jumps over the lazy dog.";
String r3 = s3.replaceAll("\\s([a-z]{4})\\s", " <b>$1</b> ");
System.out.println(r3);
}
}

单个字符的匹配规则如下:

正则表达式 规则 可以匹配
A 指定字符 A
\u548c 指定Unicode字符
. 任意字符 ab&0
\d 数字0~9 0~9
\w 大小写字母,数字和下划线 a~zA~Z0~9_
\s 空格、Tab键 空格,Tab
\D 非数字 aA&_,……
\W 非\w &@,……
\S 非\s aA&_,……

多个字符的匹配规则如下:

正则表达式 规则 可以匹配
A* 任意个数字符 空,AAAAAA,……
A+ 至少1个字符 AAAAAA,……
A? 0个或1个字符 空,A
A{3} 指定个数字符 AAA
A{2,3} 指定范围个数字符 AAAAA
A{2,} 至少n个字符 AAAAAAAAA,……
A{0,3} 最多n个字符 空,AAAAAA

复杂匹配规则主要有:

正则表达式 规则 可以匹配
^ 开头 字符串开头
$ 结尾 字符串结束
[ABC] […]内任意字符 A,B,C
[A-F0-9xy] 指定范围的字符 A,……,F0,……,9xy
[^A-F] 指定范围外的任意字符 A~F
AB|CD|EF AB或CD或EF ABCDEF

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 qubeijun的博客

赞助

  • 微信

    微信

  • 支付宝

    支付宝