当前位置:网站首页>regular expression
regular expression
2022-07-26 16:47:00 【lonesomee】
Regular expressions :
- Used to describe the content format of string , It is usually used to match whether a string meets the format requirements
- Syntax for regular expressions :
1.[]: Represents a character , This character can be [] Specified in
for example :
[abc]: This character can be a or b or c
[a-z]: Represents any lowercase letter
[a-zA-Z]: Represents any letter
[a-zA-Z0-9]: Represents any alphanumeric
[a-zA-Z0-9_]: Indicates an underscore of any number or letter
[^abc]: As long as the character is not a or b or c
2. Predefined characters :
.: Represents any character , There's no scope
\d: Represents any number , Equate to [0-9]
\w: Represents any word character , Equate to [a-zA-Z0-9_]---- Word characters refer to letters / Numbers /_
\s: Represents any white space character
\D: It's not a number
\W: Not word characters
\S: It's not a blank character
3. quantifiers :
?: It means that the previous content appears 0-1 Time
for example : [abc]? Can match :a or b or c Or write nothing
+: Indicates that the previous content appears at least 1 Time
for example : [abc]+ Can match :b or aaaaaaaaaa... or abcabcbabcbabcba....
But it doesn't match : Write nothing or abcfdfsbbaqbb34bbwer...
*: Indicates that the previous content appears any time (0- many times )--- Match content with + Agreement , Just don't write once
for example : [abc]* Can match :b or aaaaaaaaaa... or abcabcba.... Or write nothing
But it doesn't match :abcfdfsbbaqbb34bbwer...
{n}: It means that the previous content appears n Time
for example : [abc]{3} Can match :aaa or bbb or aab or abc or bbc
But it doesn't match : aaaa or aad
{n,m}: Indicates that the previous content appears at least n Most times m Time
for example : [abc]{3,5} Can match :aaa or abcab perhaps abcc
But it doesn't match :aaaaaa or aabbd
{n,}: It means that the previous content appears n More than once ( contain n Time )
for example : [abc]{3,} Can match :aaa or aaaaa.... or abcbabbcbabcba....
But it doesn't match :aa or abbdaw...
4.() For grouping , Is to see the contents in parentheses as a whole
for example : (abc){3} Express abc As a whole 3 Time . Can match abcabcabc
But it doesn't match aaa or abcabc
(abc|def){3} Express abc or def As a whole 3 Time .
Can match : abcabcabc or defdefdef or abcdefabc
But it doesn't match abcdef or abcdfbdef
String Support methods related to regular expressions :
- matches(): Use the given regular expression (regex) Verify whether the format of the current string meets the requirements , If yes, return true, Otherwise return to false
public class MatchesDemo {
public static void main(String[] args) {
String email = "[email protected]";
String regex = "[a-zA-Z0-9_][email protected][a-zA-Z0-9]+(\\.[a-zA-Z]+)+";
// Use regex matching email Whether the format requirements are met
boolean match = email.matches(regex);
if(match){
System.out.println(" Is the correct email format ");
}else{
System.out.println(" It's not the right email format ");
}
}
}
- replaceAll(): Will satisfy the regular expression in the current string (regex) Replace the part of with the given string (s)
public class ReplaceAllDemo {
public static void main(String[] args) {
String line = "abc123def456ghi78";
line = line.replaceAll("[0-9]+","#NUMBER#");
System.out.println(line);
}
}
- split(): Split the current string according to the part that satisfies the regular expression , Will split out with String[] Form to return
public class SplitDemo {
public static void main(String[] args) {
String line = "abc123def456ghi";
String[] data = line.split("[0-9]+"); // Split by number ( The numbers are removed )
System.out.println(Arrays.toString(data)); // take data The array is converted to a string and output
line = "123.456.78";
data = line.split("\\."); // Press . Demolition (. It's gone )
System.out.println(Arrays.toString(data));
// At the beginning, it is a separable item (.), Then the array number 1 Elements are empty strings ""
// If two in a row ( More than two ) Splittable items , Then an empty string will also be pulled out in the middle ""
// If there are multiple separable items at the end , Then the empty string is ignored
line = ".123.456..78.......";
data = line.split("\\."); // Press . Demolition (. It's gone )
System.out.println(Arrays.toString(data));
}
}边栏推荐
- 理财产品锁定期是什么意思?理财产品在锁定期能赎回吗?
- Digital intelligence transformation, management first | jnpf strives to build a "full life cycle management" platform
- TCP 和 UDP 可以使用相同端口吗?
- 第一章概述-------第一节--1.3互联网的组成
- 搭建typora图床
- Alibaba side: analysis of ten classic interview questions
- NUC 11 build esxi 7.0.3f install network card driver-v2 (upgraded version in July 2022)
- 怎么使用C语言嵌套链表实现学生成绩管理系统
- Thinkphp历史漏洞复现
- JD Sanmian: I want to query a table with tens of millions of data. How can I operate it?
猜你喜欢

该怎么写单元测试呢

数字化转型怎么就那么的难?!

Matlab paper illustration drawing template issue 40 - pie chart with offset sector

什么是分布式定时任务框架?

Linux安装mysql8.0.29详细教程

How does win11 reinstall the system?

PyQt5快速开发与实战 3.2 布局管理入门 and 3.3 Qt Designer实战应用

Marketing guide | several common micro blog marketing methods

Digital intelligence transformation, management first | jnpf strives to build a "full life cycle management" platform
最终一致性性分布式事务 TCC
随机推荐
kubernetes之ConfigMap
Why is digital transformation so difficult?!
视频媒介video
MySQL lock mechanism (example)
怎么使用C语言嵌套链表实现学生成绩管理系统
C#读取本地文件夹中所有文件文本内容的方法
Wechat applet - network data request
什么是分布式定时任务框架?
京东三面:我要查询千万级数据量的表,怎么操作?
极大似然估计
2022 Niuke summer multi school training camp 2 (bdghjkl)
综合设计一个OPPE主页--导航栏的设计
抓包与发流软件与网络诊断
Alibaba side: analysis of ten classic interview questions
别用Xshell了,试试这个更现代的终端连接工具
Set up typera drawing bed
Response object - response character data
Replicationcontroller and replicaset of kubernetes
The first case in Guangdong! A company in Guangzhou was punished by the police for failing to fulfill its obligation of data security protection
【开发教程8】疯壳·开源蓝牙心率防水运动手环-三轴计步伐