1. Representing characters
2, the original string
Python中字符串前面加上 r 表示原生字符串
,
As with most programming languages, 正则表达式里使用"\"作为转义字符
, this can cause backslash trouble. If you need to match the character "\" in the text, then 4 backslashes "\\" will be required in the regular expression expressed in the programming language: the first two and the last two are used for escaping in the programming language into a backslash, converted to two backslashes and then escaped into a single backslash in the regular expression.
The native string in Python solves this problem very well. With the original string, you no longer have to worry about missing a backslash, and the written expression is more intuitive.
3. Indicate the quantity
4. Representing boundaries
5. Match the group
6、
re.match function
re.match attempts to match a pattern from the beginning of the string. If the match is not successful at the beginning, match() returns none.
7、
re.search method
re.search scans the entire string and returns the first successful match.
Function syntax:
re.search(pattern,string, flags=0)
The difference between re.match and re.search
re.match only matches the beginning of the string. If the beginning of the string does not match the regular expression, the match fails and the function returns None; while re.search matches the entire string until a match is found.
8、
search and replace
Python's re module provides re.sub for replacing matches in strings.
grammar:
9、
compile function
The compile function is used to compile the regular expression and generate a regular expression ( Pattern ) object for use by the match() and search() functions.
The syntax format is:
10、
findall
Finds all substrings in the string matched by the regular expression and returns a list, or an empty list if no matches are found.
Note: match and search are to match once and findall to match all.
The syntax format is:
findall(string[, pos[, endpos]])
11、
re.split
The split method returns a list after splitting the string according to the substrings that can be matched. Its usage is as follows: