当前位置:网站首页>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));
}
}边栏推荐
- Linux安装mysql8.0.29详细教程
- 接口比较器
- Operating system migration practice: deploying MySQL database on openeuler
- Response对象-响应字符数据
- 量化交易之数字货币篇 - 通过时间戳与方向来合并逐笔成交数据(大单合并)
- Linux Installation mysql8.0.29 detailed tutorial
- kubernetes之探针
- Marxan model, reserve optimization and protection vacancy selection technology, application in invest ecosystem
- Want the clouds in the picture to float? Video editing services can be achieved in three steps with one click
- 第一章概述-------第一节--1.3互联网的组成
猜你喜欢

The difference and efficiency comparison of three methods of C # conversion integer

限流对比:Sentinel vs Hystrix 到底怎么选?

My SQL is OK. Why is it still so slow? MySQL locking rules

C#转整型的三种方式的区别以及效率对比

DTS is equipped with a new self-developed kernel, which breaks through the key technology of the three center architecture of the two places Tencent cloud database
Final consistency distributed transaction TCC

Differences between the use of structs and classes

Win11 how to close a shared folder

Re8: reading papers Hier spcnet: a legal stat hierarchy based heterogeneous network for computing legal case

Matlab paper illustration drawing template issue 40 - pie chart with offset sector
随机推荐
Response对象-响应字符数据
公安部发出暑期旅游客运交通安全预警:手握方向盘 绷紧安全弦
Win11如何关闭共享文件夹
【E-MR】NameNode的错误恢复记录
What is a distributed timed task framework?
Tcpdump命令详解
Singleton mode
Win11 auto delete file setting method
综合设计一个OPPE主页--顶部,头部的设计
docker安装redis?如何配置持久化策略?
"Green is better than blue". Why is TPC the last white lotus to earn interest with money
How to balance open utilization and privacy security compliance of public data?
Re7: reading papers fla/mlac learning to predict charges for critical cases with legal basis
该怎么写单元测试呢
综合设计一个OPPE主页--布局与初始化
视频媒介video
Tdengine landed in GCL energy technology, with tens of billions of data compressed to 600gb
Video media video
2022牛客暑期多校训练营2(BDGHJKL)
Vscode batch delete