当前位置:网站首页>Regular expression (I)
Regular expression (I)
2022-07-04 09:36:00 【Bomapple】
First , When we register or log in on the web page, we often see some format error prompts .
such as : You are registering 163 When you account Register Netease free email - China's largest e-mail service provider Enter the email address, and the system will prompt you . Your account is too long or you are not allowed to use Chinese and so on .
This kind of operation can be completed if we use our existing knowledge points . But the effect of completion is not good . It's not easy to write . Especially for mailbox matching . Phone number matching .
Regular expressions are expressions designed to deal with similar problems . English full name : Regular Expression. abbreviation regex perhaps re. But you need to know that we are using python Of re Module before . First of all, we should have a certain understanding of regularity . Like we use time Module before . We have a certain understanding of time .
Regular expressions are right String manipulation A kind of Logical formula . We usually use regular expressions for character string Conduct matching and Filter . Advantages and disadvantages of using regularization :
advantage : flexible , Functional , Strong logic .
shortcoming : Get started Great difficulty . Once you get started , I can't put it down
Tools : All major text editors generally have regular matching function . We can also go to Regular expression online test - Webmaster Tools Do online testing .
Metacharacters
Metacharacters : Metacharacters are the soul of regular expressions . There is too much content in metacharacters , Here we only introduce some commonly used . Can match a single text symbol
1. Character set
Character groups are easy to use [] Cover up . stay [] The content appearing in will be matched . for example :[abc] matching a or b or c
If there is too much content in the character group, you can also use - , for example : [a-z] matching a To z All the letters between [0-9] Match all Arabic numerals
reflection : [a-zA-Z0-9] What matches ?
2. Basic metacharacters
Basic metacharacters . There are a lot of things on the Internet . Just a few, but :
. Matches any character other than a newline character
\w Match letters or numbers or underscores
\s Match any whitespace
\d Match the Numbers
\n Match a line break
\t Match a tab
^ Matches the beginning of the string
$ Match the end of the string
\W Match non letters or numbers or underscores
\D Match non numeric
\S Match non whitespace
a|b Matching character a Or character b
() Match the expression in brackets , It also means a group
[...] Match characters in a character set
[^...] Matches all characters except those in the character group
3. quantifiers
All the content we have matched so far is a single character symbol . How to match many characters at one time , We need to use quantifiers
Be careful : Quantifiers control the number of times the preceding metacharacter appears
* Repeat zero or more times
+ Repeat one or more times
? Repeat zero or one time
{n} repeat n Time
{n,} repeat n Times or more
{n,m} repeat n To m Time
4. Lazy matching and greedy matching
In quantifiers ?,*, +,{} All belong to greedy matching . Is to match as many results as possible .
character string : <p> What do you think , If you don't accept me, do me </p><p> Beat you </p>
Regular expressions : <.*>( Greedy matching )
result : <p> What do you think , If you don't accept me, do me </p><p> Beat you </p>
Regular expressions : <.*?>( Inertia matching )
result : <p> What do you think , If you don't accept me, do me </p>
5. Packet matching
Use in regular () Grouping . such as . We need to match a simple email . stay @ You can match any 6-20 Irregular letters or numbers .
[a-zA-Z0-9]{6,}@163.com
6. Escape character
# Not transferred
print('D:\Desktop\notebook')
# Ordinary escape
print('D:\Desktop\\notebook')
#r-string
print(r'D:\Desktop\notebook')
D:\Desktop
otebook
D:\Desktop\notebook
D:\Desktop\notebook
边栏推荐
- Daughter love in lunch box
- Mantis creates users without password options
- 《网络是怎么样连接的》读书笔记 - WEB服务端请求和响应(四)
- C # use ffmpeg for audio transcoding
- What is uid? What is auth? What is a verifier?
- Global and Chinese market of wheel hubs 2022-2028: Research Report on technology, participants, trends, market size and share
- The 14th five year plan and investment risk analysis report of China's hydrogen fluoride industry 2022 ~ 2028
- 如何编写单元测试用例
- Report on investment analysis and prospect trend prediction of China's MOCVD industry Ⓤ 2022 ~ 2028
- 浅谈Multus CNI
猜你喜欢
2022-2028 global protein confectionery industry research and trend analysis report
The child container margin top acts on the parent container
HMS core helps baby bus show high-quality children's digital content to global developers
Kubernetes CNI 插件之Fabric
2022-2028 global optical transparency industry research and trend analysis report
ArrayBuffer
How to batch change file extensions in win10
If you can quickly generate a dictionary from two lists
法向量点云旋转
回复评论的sql
随机推荐
Global and Chinese trisodium bicarbonate operation mode and future development forecast report Ⓢ 2022 ~ 2027
Lauchpad X | 模式
You can see the employment prospects of PMP project management
Summary of the most comprehensive CTF web question ideas (updating)
If you can quickly generate a dictionary from two lists
Mantis creates users without password options
Global and Chinese market of sampler 2022-2028: Research Report on technology, participants, trends, market size and share
回复评论的sql
HMS core helps baby bus show high-quality children's digital content to global developers
How does idea withdraw code from remote push
GoLand environment variable configuration
Reading notes of how the network is connected - understanding the basic concepts of the network (I)
Write a jison parser (7/10) from scratch: the iterative development process of the parser generator 'parser generator'
Write a jison parser from scratch (2/10): learn the correct posture of the parser generator parser generator
法向量点云旋转
Les différents modèles imbriqués de listview et Pageview avec les conseils de flutter
PHP student achievement management system, the database uses mysql, including source code and database SQL files, with the login management function of students and teachers
PMP registration process and precautions
China electronic grade sulfur trioxide Market Forecast and investment strategy report (2022 Edition)
Lauchpad x | MODE