当前位置:网站首页>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

原网站

版权声明
本文为[Bomapple]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141424300897.html