当前位置:网站首页>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));
}
}边栏推荐
- 【E-MR】NameNode的错误恢复记录
- Set up typera drawing bed
- Win11 how to close a shared folder
- A preliminary understanding of MVC and ECS design architectures
- What does it mean to lock financial products regularly? Can financial products be redeemed during the lock-in period?
- About the idea plug-in I wrote that can generate service and mapper with one click (with source code)
- movable-view 组件(可上下左右拖动 )
- kubernetes之ConfigMap
- 【开发教程8】疯壳·开源蓝牙心率防水运动手环-三轴计步伐
- Marxan模型保护区优化与保护空缺甄选技术、InVEST生态系统中的应用
猜你喜欢

【开发教程8】疯壳·开源蓝牙心率防水运动手环-三轴计步伐

Linux安装mysql8.0.29详细教程

How can win11 system be reinstalled with one click?

Tcpdump命令详解

Win11 how to close a shared folder

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

【飞控开发基础教程2】疯壳·开源编队无人机-定时器(LED 航情灯、指示灯闪烁)
![Sharing of 40 completed projects of high-quality information management specialty [source code + Thesis] (VI)](/img/b9/629449d3c946b017075ed42eaa81bf.png)
Sharing of 40 completed projects of high-quality information management specialty [source code + Thesis] (VI)

Tdengine landed in GCL energy technology, with tens of billions of data compressed to 600gb

Comprehensively design an oppe homepage -- the design of the top and head
随机推荐
It turns out that cappuccino information security association does this. Let's have a look.
[e-mr] error recovery record of namenode
接口比较器
带你一分钟了解对称加密和非对称加密
Set up typera drawing bed
【开发教程8】疯壳·开源蓝牙心率防水运动手环-三轴计步伐
[fluent -- advanced] packaging
工作流引擎在vivo营销自动化中的应用实践
导数、微分、偏导数、全微分、方向导数、梯度的定义与关系
抓包与发流软件与网络诊断
6种方法帮你搞定SimpleDateFormat类不是线程安全的问题
2022-2023 topic recommendation of information management graduation project
PyQt5快速开发与实战 3.4 信号与槽关联
Win11怎么重新安装系统?
Happy 10th birthday, clojure
Matlab论文插图绘制模板第40期—带偏移扇区的饼图
Vscode batch delete
Marxan模型保护区优化与保护空缺甄选技术、InVEST生态系统中的应用
2022 Niuke summer multi school training camp 2 (bdghjkl)
40个高质量信息管理专业毕设项目分享【源码+论文】(六)