Objective
Recently in a function with Django, mainly for the realization of machine monitoring, the process, involving the URL to identify IP routing problems, the beginning with a variety of expressions can not be properly routed to the IP, indicating that they have a lot of unfamiliar with this piece of the place, This article is mainly for Python's regular expression to expand the introduction, to make memo.
Python Regular description regular match
As you learned about Python, there is a module in Python that has a regular expression: Re. The regular matching pattern mainly has the following categories:
x? 匹配0个或1个x,最多1个x+ 匹配1个或多个x,至少1个x* 匹配0个或多个xx{m,n} 匹配m与n之间个数的x,如{2,4},则可匹配到xx,xxx,xxxxx|y 匹配x或y^ 匹配文本开头$ 匹配文本结尾[a-zA-Z] 匹配26个字母的大小写[^a-zA-Z] 匹配非26个字母的其它字符[0-9] 匹配0-9之间的所有数字
If I want to match an email address, what should I do, let's look at the following code:
import re x="[a-z][email protected][a-z]+.[a-z]+" s1='[email protected]'s2='liran#devarea.com' c=re.match(x,s1)if c: print ('ok')else: print ('no')
First define a pattern matching string [a-z][email protected][a-z]+.[a-z]+ , the main meaning of this pattern string is as follows:
Match a substring of a letter consisting of at least 1 letters of a –[a-z]+
Match a symbol –@
Match a substring of a letter consisting of at least 1 letters of a –[a-z]+
Match a punctuation –.
Match a substring of a letter consisting of at least 1 letters of a –[a-z]+
As can be seen from the above, the other matching method is very simple, can match some simple mode of the message, if the message contains numbers and uppercase letters, it is also very simple, as long as the match to add a number matching the line, as follows:
x="[a-zA-Z0-9][email protected][a-zA-Z0-9]+.[a-zA-Z0-9]+"
If you want to match the mail address of a particular suffix, it is also relatively simple, directly at the end of the pattern string with a specific suffix of the substring on the line, such as we want to match. Com,net and the like, we can match them in the following ways:
x=[a-zA-Z0-9]+@[a-z0-9]+.(net|com)
There are more complex ways, such as e-mail addresses that contain some special characters, as follows:
pattern="^[A-Za-z0-9\.\+_-][email protected][A-Za-z0-9\._-]+\.[a-zA-Z]*$"
In addition to the email address, we often need to determine such as phone number, IP address and so on, as follows:
IP Address
ip="^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"
Phone number
="'(\d{3}[-\.\s]??\d{3}[-\.\s]??\d{4}|\(\d{3}\)\s*\d{3}[-\.\s]??\d{4}|\d{3}[-\.\s]??\d{4})'"
There is also the possibility that you need to go to a URL link in the crawler, as follows:
url='http[s]?://(?:[a-zA-Z]|[0-9]|[[email protected]&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
Regular lookup
In the actual Python application, we also often use the regular lookup function, such as finding the first occurrence of text in the number, can be used as follows:
str='hello,this is 888, my first regular expression 999 exercise'= re.search(r"[0-9]+"str)if m: print('匹配: ',m.group()) #输出: 888
If you want to match all the numbers in the text, you can use the findall function as follows:
str='hello,this is 888, my first regular expression 999 exercise'm=re.findall(r"[0-9]+",str)print m #输出: ['888','999']
So it's easy to find the desired substring with re.
Regular replacement
In actual use, regular replacement is also very common, with re sub函数 can also be easily solved.
Python regular-expression learning