当前位置:网站首页>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));
}
}边栏推荐
- 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
- How to balance open utilization and privacy security compliance of public data?
- 面试时候常说的复杂度到底是什么?
- 别用Xshell了,试试这个更现代的终端连接工具
- The process and harvest of developing browser plug-ins with clojurescript
- Win11 auto delete file setting method
- 数字化转型怎么就那么的难?!
- vscode批量删除
- Matlab paper illustration drawing template issue 40 - pie chart with offset sector
- 理财产品锁定期是什么意思?理财产品在锁定期能赎回吗?
猜你喜欢

vlang捣鼓之路

Win11 auto delete file setting method

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

Use verdaccio to build your own NPM private library

Chapter 1 Overview - Section 1 - 1.3 composition of the Internet

Tcpdump命令详解

"Green is better than blue". Why is TPC the last white lotus to earn interest with money

【开发教程7】疯壳·开源蓝牙心率防水运动手环-电容触摸

Marxan model, reserve optimization and protection vacancy selection technology, application in invest ecosystem

Understanding JS foundation and browser engine
随机推荐
Use verdaccio to build your own NPM private library
How to test the circle of friends (mind map)
How to use C language nested linked list to realize student achievement management system
Digital currency of quantitative transactions - merge transaction by transaction data through timestamp and direction (large order consolidation)
Operating system migration practice: deploying MySQL database on openeuler
Definition and relationship of derivative, differential, partial derivative, total derivative, directional derivative and gradient
导数、微分、偏导数、全微分、方向导数、梯度的定义与关系
【Flutter -- 进阶】打包
Linux Installation mysql8.0.29 detailed tutorial
Packet capturing and streaming software and network diagnosis
Win11 how to close a shared folder
Tao and art of R & D Efficiency - Tao chapter
Win11怎么重新安装系统?
How to write unit tests
The difference between anonymous methods and lambda expressions
IDEA 阿里云多模块部署
srec_ Use of common cat parameters
From SiCp to LISP video replay
About the idea plug-in I wrote that can generate service and mapper with one click (with source code)
什么是分布式定时任务框架?