Regular Expression Study Notes

Theoretical part:

To use python's regular expression function, you need to call the re module. The re module provides regular expression tools for advanced string processing. The following functions can be called:

1. Function introduction

1. re.match function

Function: re.match attempts to match a pattern from the starting position of the string. If the matching is not successful at the starting position, match() returns none.

Syntax: re.match(pattern,string,flags=0)

Returns a matching object if the match is successful, otherwise returns None.

2. re.search function

Function: re.search scans the entire string and returns the first successful match.

Syntax: re.search(pattern, string, flags=0)

If the match is successful, the re.search method returns a matching object, otherwise it returns None.

3. re.sub function

Function: re.sub is used to replace matches in a string.

语法:re.sub(pattern, repl, string, count=0, flags=0)

The repl parameter can be a replacement string or a function.

4. re.compile function

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.

Syntax: re.compile(pattern[, flags])

Returns a Match object if the match is successful.

5. findall function

Function: Find all substrings matched by the regular expression in the string, and return a list, if no match is found, return an empty list.

Syntax: findall(string[, pos[, endpos]])

6. re.finditer function

Function: Find all substrings matched by regular expression in a string and return them as an iterator.

Syntax: re.finditer(pattern, string, flags=0)

7. re.split function

Function: The split method returns a list after splitting the string according to the substrings that can be matched.

Syntax: re . split ( pattern , string [, maxsplit = 0 , flags = 0 ])

Note: For detailed function parameter information, please refer to the link: http://www.runoob.com/python/python-reg-expressions.html#flags

 

Second, the function analysis

1. Discrimination: the difference between match and search

re.match only matches the beginning of the string. If the beginning of the string does not conform to the regular expression, the match fails and the function returns None;

re.search matches the entire string until a match is found.

example:

2. Discrimination: 3 matching functions match, search, findall

match and search are to match once, findall to match all.

 

Third, the regular expression object

1. re.RegexObject

The function re.compile() returns a RegexObject object.

2. re.MatchObject

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325215300&siteId=291194637