当前位置:网站首页>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
边栏推荐
- Research and investment strategy report of China's electronic hydrogen peroxide industry (2022 Edition)
- Analysis report on the production and marketing demand and investment forecast of tellurium dioxide in the world and China Ⓣ 2022 ~ 2027
- Explanation of for loop in golang
- 2022-2028 global optical transparency industry research and trend analysis report
- pcl::fromROSMsg报警告Failed to find match for field ‘intensity‘.
- Write a jison parser from scratch (2/10): learn the correct posture of the parser generator parser generator
- Reading notes of how the network is connected - understanding the basic concepts of the network (I)
- Multilingual Wikipedia website source code development part II
- Service call feign of "micro service"
- 2022-2028 global intelligent interactive tablet industry research and trend analysis report
猜你喜欢
C # use gdi+ to add text with center rotation (arbitrary angle)
2022-2028 global visual quality analyzer industry research and trend analysis report
Sword finger offer 30 contains the stack of Min function
How do microservices aggregate API documents? This wave of show~
If you can quickly generate a dictionary from two lists
Mantis creates users without password options
2022-2028 global small batch batch batch furnace industry research and trend analysis report
After unplugging the network cable, does the original TCP connection still exist?
Target detection -- intensive reading of yolov3 paper
ASP. Net to access directory files outside the project website
随机推荐
Report on research and investment prospect prediction of China's electronic grade sulfuric acid industry (2022 Edition)
2022-2028 global small batch batch batch furnace industry research and trend analysis report
HMS core helps baby bus show high-quality children's digital content to global developers
Solution to null JSON after serialization in golang
Luogu deep foundation part 1 Introduction to language Chapter 4 loop structure programming (2022.02.14)
What is permission? What is a role? What are users?
Global and Chinese trisodium bicarbonate operation mode and future development forecast report Ⓢ 2022 ~ 2027
Global and Chinese market of sampler 2022-2028: Research Report on technology, participants, trends, market size and share
2022-2028 global gasket plate heat exchanger industry research and trend analysis report
If you can quickly generate a dictionary from two lists
SSM online examination system source code, database using mysql, online examination system, fully functional, randomly generated question bank, supporting a variety of question types, students, teache
GoLand environment variable configuration
【leetcode】29. Divide two numbers
The child container margin top acts on the parent container
After unplugging the network cable, does the original TCP connection still exist?
什么是权限?什么是角色?什么是用户?
Report on investment analysis and prospect trend prediction of China's MOCVD industry Ⓤ 2022 ~ 2028
Flutter 小技巧之 ListView 和 PageView 的各種花式嵌套
Report on the development trend and prospect trend of high purity zinc antimonide market in the world and China Ⓕ 2022 ~ 2027
Basic data types in golang