当前位置:网站首页>regular expression
regular expression
2022-07-26 20:12:00 【Code ancestor】
Regular expressions
【 teacher 】 Yangzhou teaching director - Mr. Zeng 9:24:32
^[a-zA-Z0-9_-][email protected][a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$
【 teacher 】 Yangzhou teaching director - Mr. Zeng 9:24:49
^1(3[0-9]|5[0-3,5-9]|7[1-3,5-8]|8[0-9])\d{8}$
【 teacher 】 Yangzhou teaching director - Mr. Zeng 9:24:57
^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$
【 teacher 】 Yangzhou teaching director - Mr. Zeng 9:25:05
^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[[email protected]#$%^&*? ]).*$

Rule string
password : Length not less than 8 position , Include case , Numbers , Special symbols
Phone number :138 139 158 159 170 189…… after 8 Bit arbitrary
mailbox : [email protected]
Id card number : xxxxxxYYYYMMDDxxxx
How to write regular
How is regular checked
Strings that conform to certain rules .
Common rules
A: character
x character x. give an example :'a' According to the character a
\\ Backslash character Express \.
\n New line ( Line break ) operator ('\u000A')
\r A carriage return ('\u000D')
B: Character class , Match symbol []
[abc] a、b or c( Simple class )
[^abc] Any character , except a、b or c( no )
[a-zA-Z] a To z or A To Z, Both ends of the letter are included ( Range )
[0-9] 0 To 9 The characters include
C: Predefined character class
. Any character . All I have is . Character itself , How to express it ? \.
\d Numbers :[0-9]
\w Word characters :[a-zA-Z_0-9]
The things that make up words in regular expressions must have these things
"http://www.baidu.com" ^[http] [com]$
D: Boundary matcher
^ The beginning of a row
$ End of line
\b Word boundaries
It's not where the words and characters are .
give an example :hello world?haha;xixi
In regular expressions , If the characters are given directly , It's the exact match . use \d Can match a number ,\w Can match a letter or number , therefore :
'00\d'Can match'007', But it doesn't match'00A';'\d\d\d'Can match'010';'\w\w'Can match'js';
. Can match any character , therefore :
'js.'Can match'jsp'、'jss'、'js!'wait .
To match a longer character , In regular expressions , use * Represents any character ( Include 0 individual ), use + Represents at least one character , use ? Express 0 Or 1 Characters , use {n} Express n Characters , use {n,m} Express n-m Characters :
Let's take a complex example :\d{3}\s+\d{3,8}.
Let's read it from left to right :
\d{3}Represents a match 3 A digital , for example'010';\sCan match a space ( Also include Tab Equal space character ), therefore\s+Indicates that there is at least one space , For example, match' ','\t\t'etc. ;\d{3,8}Express 3-8 A digital , for example'1234567'.
combined , The regular expression above can match the phone number with area code separated by any space .
If you want to match '010-12345' Such a number ? because '-' Is a special character , In regular expressions , Use '\' escape , therefore , The rule above is \d{3}\-\d{3,8}.
Advanced
To make a more accurate match , It can be used [] Scope of representation , such as :
[0-9a-zA-Z\_]Can match a number 、 Letters or underscores ;[0-9a-zA-Z\_]+Can be matched by at least one number 、 A string of letters or underscores , such as'a100','0_Z','js2015'wait ;[a-zA-Z\_\$][0-9a-zA-Z\_\$]*Can be matched by letters or underscores 、$ start , Followed by any number by 、 Letters or underscores 、$ Composed string , That is to say JavaScript Allowed variable names ;[a-zA-Z\_\$][0-9a-zA-Z\_\$]{0, 19}A more precise limit on the length of a variable is 1-20 Characters ( front 1 Characters + The most behind 19 Characters ).
A|B Can match A or B, therefore (J|j)ava(S|s)cript Can match 'JavaScript'、'Javascript'、'javaScript' perhaps 'javascript'.
^ Indicates the beginning of a line ,^\d Indicates that it must start with a number .
$ Indicates the end of the line ,\d$ Indicates that it must end with a number .
You may have noticed ,js It can also match 'jsp', But add ^js$ It becomes a whole line match , It can only match 'js' 了 .
E:Greedy quantifiers
X? X, Not once or once
X* X, Zero or more times
X+ X, Once or more
X{n} X, just n Time
X{n,} X, At least n Time
X{n,m} X, At least n Time , But no more than m Time
Write a test , Enter the mobile phone number to judge whether it meets the requirements .
public class RegexDemo {
public static void main(String[] args) {
// Input from keyboard 5-10 Digit number , Can't use 0 start
Scanner scanner = new Scanner(System.in);
System.out.println(" Please enter 5-10 Is the number ");
String str = scanner.next();
// Create regular objects
String regex = "[1-9][0-9]{4,9}";
boolean result = str.matches(regex);
if (result){
System.out.println(" The content you entered meets the requirements ");
}else {
System.out.println(" Unqualified ");
}
}
}
Write a test , Let the user enter a mobile number , Then judge whether the mobile phone number entered by the user conforms to the rules
public class RegexDemo {
public static void main(String[] args) {
// Input from keyboard 5-10 Digit number , Can't use 0 start
Scanner scanner = new Scanner(System.in);
System.out.println(" Please enter your mobile number ");
String str = scanner.next();
// Create regular objects
String regex = "1[3-9]\\d{9}";
boolean result = str.matches(regex);
if (result){
System.out.println(" The content you entered meets the requirements ");
}else {
System.out.println(" Unqualified ");
}
}
}
The regular expression of the mailbox matches
[email protected]
[email protected]
[email protected]
[email protected]
public class RegexDemo {
public static void main(String[] args) {
// Input from keyboard 5-10 Digit number , Can't use 0 start
Scanner scanner = new Scanner(System.in);
System.out.println(" Please enter email address ");
String str = scanner.next();
// Create regular objects
String regex = "\\[email protected]\\w{2,8}(.\\w{2,3})+";
boolean result = str.matches(regex);
if (result){
System.out.println(" The content you entered meets the requirements ");
}else {
System.out.println(" Unqualified ");
}
}
}
边栏推荐
- 金仓数据库 KingbaseES SQL 语言参考手册 (12. SQL语句:ALTER LANGUAGE 到 ALTER SUBSCRIPTION)
- 金仓数据库 KingbaseES SQL 语言参考手册 (19. SQL语句: DROP TABLE 到 LOAD)
- 2000字助你精通防抖与节流
- 同花顺靠谱吗?我刚开始学习理财,开证券账户安全吗?
- 一家芯片公司倒在了B轮
- How to adjust the abnormal win11 USB drive to normal?
- Vite 配置 Eslint 规范代码
- C # use the default transformation method
- 罗永浩赌上最后一次创业信用
- numpy.newaxis
猜你喜欢

Canvas graphics

Week 6 Convolutional Neural Networks (CNNs)

操作系统常见面试题目总结,含答案

Docker使用mysql:5.6和 owncloud 镜像,构建一个个人网盘,安装搭建私有仓库 Harbor

Decompile jar files (idea environment)

网络与VPC动手实验

Household deposits increased by 10.33 trillion yuan in the first half of the year, with an average of 57.1 billion deposits pouring into banks every day

3万脱发人,撑起一个IPO

Overview of canvas

直播预约有奖| 高级咨询顾问徐雁斐:效能度量如何助力高效精细的外包管理
随机推荐
答应我,看完别再写狗屎代码了。。
Analysis of interface testing
Kingbasees SQL language reference manual of Jincang database (12. SQL statement: alter language to alter subscription)
Svn - detailed documentation
C#异步编程看这篇就够了
KVM virtualization
go+mysql+redis+vue3简单聊室,第5弹:使用消息队列和定时任务同步消息到mysql
Kingbases SQL language reference manual of Jincang database (16. SQL statement: create sequence to delete)
移动端video兼容你需要知道的几点
Household deposits increased by 10.33 trillion yuan in the first half of the year, with an average of 57.1 billion deposits pouring into banks every day
I tried many report tools and finally found a report based on Net 6
计算机专业面试题目总结,总导航
花1200亿修一条“地铁”,连接4个万亿城市,广东在想啥?
Software testing - what are the automated testing frameworks?
.NET GC工作流程
金仓数据库 KingbaseES SQL 语言参考手册 (13. SQL语句:ALTER SYNONYM 到 COMMENT)
有点酷,使用 .NET MAUI 探索太空
以 NFT 为抵押品的借贷协议模式探讨
一文读懂 .NET 中的高性能队列 Channel
numpy.newaxis