The content of this article is about how to use the re module in python regular expressions? The introduction to the usage of the re module has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Regular expressions are the most commonly used method for processing strings. Regular expressions can be seen everywhere in our coding.
The regular expressions in python are slightly different from those in other languages:
1. When replacing a string, the replaced string can be a function
2 , the split function can specify the number of splits, which will lead to pitfalls
3. The expression defined in the previous item must be of a fixed length
The following is a detailed description of how to use the re module. In fact, pay attention The three differences mentioned above are just fine
1, match
Description:
re.match attempts to match from the beginning of the string If a pattern is not matched successfully at the starting position, match() will return None.
Syntax:
re.match(pattern, string, flags=0)
flags are optional identifiers, multiple identifiers can be obtained by bitwise or (|) specified. For example, re.I | re.M is set to I and M identifiers:
##Modifier |
Description |
##re.I
| Make the match case-insensitive
|
Multi-line matching, affects ^ and $ |
|
make. Match all characters including newlines |
|
# Parse characters according to the Unicode character set. This flag affects \w, \W, \b, \B. |
##re.X |
This flag makes writing regular expressions easier to understand by giving you a more flexible format. |
The re.match method returns a matching object if the match is successful. You can use the group(num) or groups() matching object function to obtain the matching expression. group() or group(0), returns the matching result of the entire regular expression. Example: 2, search Instructions: re.search scans the entire character The concatenation returns the first successful match. Syntax: re.search(pattern, string, flags=0) The re.search method returns a matching object if the match is successful, otherwise it returns None. Match expressions can be obtained using the group(num) or groups() match object functions. Example: groupdict groupdict Returns a dictionary of all matching named subgroups. 3, sub and subn Description: re.sub is used to replace matching items in the string. re.subn returns a tuple containing the replaced string and the number of replacements. Syntax: sub(pattern, repl, string, count=0, flags=0) repl: The string to be replaced can also be a function. count: The maximum number of substitutions after pattern matching. The default value is 0, which means replacing all matches. Example: 4, compile Explanation: re.compile is used for compilation Regular expression, generates a regular expression (Pattern) object for use by the two functions match() and search(). Grammar: compile(pattern, flags=0) Example: 5, findall Description: re.findall searches for all substrings matched by the regular expression in the string and returns a list. If no match is found, an empty list is returned. Syntax: findall(pattern, string, flags=0) Example: 6, finditer Description: finditer is similar to findall, it searches for all substrings matched by the regular expression in the string and returns them as an iterator. Grammar: finditer(pattern, string, flags=0) Example: 7、 split Description: The re.split method splits the string according to the matching substrings and returns a list. Syntax: split(pattern, string, maxsplit=0, flags=0) maxsplit: number of separations, maxsplit=1 separates once, default is 0, no Limit the number of times. Example: 8, escape Explanation: re.escape for string The special strings inside are escaped. Grammar: escape(pattern) Example: 9, regular expression "(?P "(?P=name)": refers to the string matched by the group whose alias is 10. Specialities in the definition of the antecedent (negation) The common definition of the antecedent (?<=exp) and the negative definition of the antecedent (? Related recommendations: What is the use of the logging module in python? Introduction to the usage of the logging module Detailed introduction to the re regular expression of the python module |
The above is the detailed content of What are the ways to use the re module in python regular expressions? Introduction to the usage of re module. For more information, please follow other related articles on the PHP Chinese website!