当前位置:网站首页>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 ");
}
}
}
边栏推荐
- 靠元宇宙和NFT,天下秀疯狂“割韭菜”?
- These 22 drawing (visualization) methods are very important and worth collecting!
- 金仓数据库 KingbaseES SQL 语言参考手册 (19. SQL语句: DROP TABLE 到 LOAD)
- 金仓数据库 KingbaseES SQL 语言参考手册 (18. SQL语句: DROP MATERIALIZED VIEW 到 DROP SYNONYM)
- 金仓数据库 KingbaseES SQL 语言参考手册 (20. SQL语句: MERGE 到 VALUES)
- 同花顺靠谱吗?我刚开始学习理财,开证券账户安全吗?
- three.js 给地球加标签和弹窗
- 【OBS】解决OBS推两个rtmp流 + 带时间戳问题
- C#将PDF文件转成图片
- 金仓数据库 KingbaseES SQL 语言参考手册 (17. SQL语句: DISCARD 到 DROP LANGUAGE)
猜你喜欢
随机推荐
SQL优化的一些建议,希望可以帮到和我一样被SQL折磨的你
模拟身份验证
KVM virtualization
Software testing - what are the automated testing frameworks?
用 QuestPDF操作生成PDF更快更高效!
【Pytorch进阶】pytorch模型的保存与使用
【机器学习】变量间的相关性分析
Live video source code to achieve the advertising effect of scrolling up and down
Kingbases SQL language reference manual of Jincang database (16. SQL statement: create sequence to delete)
LeetCode_回溯_中等_40.组合总和 II
记一次 .NET 某物管后台服务 卡死分析
高瓴加入的PRI,君联、华控、盛世、绿动等百家机构都杀进去了
Excel-VBA 快速上手(十、提示框、可输入的弹出框)
千亿酸奶赛道,乳企巨头和新品牌打响拉锯战
猎聘问卷星,成为微信「寄生虫」
金仓数据库 KingbaseES SQL 语言参考手册 (20. SQL语句: MERGE 到 VALUES)
Kingbases SQL language reference manual of Jincang database (13. SQL statement: alter synonym to comment)
Kingbases SQL language reference manual of Jincang database (14. SQL statement: commit to create language)
同花顺靠谱吗?我刚开始学习理财,开证券账户安全吗?
计算机网络常见面试题目总结,含答案









