(a) regular expression:
a generic string expression framework;
An expression that simply expresses a set of strings;
A tool for expressing "concise" and "characteristic" ideas for strings;
Determining the attribution of a character string;
The common role of regular expressions in text:
Express the characteristics of the text type (virus);
Find or replace a set of strings at the same time;
Match all or part of a string;
The use of regular expressions:
Compile: Converts a string that conforms to the regular expression syntax to a regular expression feature.
(two) the syntax of regular expressions
(3) Regularly used operators
(d) regular expression examples
(5) The main function of the re library
Regular expression representation type
(1) raw string type (native string type)
The re library uses the raw string type to represent a regular expression, denoted as r'text' .
For example: Postal code in the mainland : r'[1-9]\d{5}'
Domestic phone number: r'\d{3}-\d{8}|\d{4}-\d{7}'
A native string type is a string that does not contain an escape character.
(2) string type, more cumbersome
For example '[1-9]\\d{5}'
'\\d{3}-\\d{8}|\\d{4}-\\d{7}'
So when the regular expression contains escape characters, use raw string .
1, re.search (pattern, string, flags=0)
Search for a first position in a string that matches the regular expression and return a match object.
Pattern: a representation of a regular expression string or a native string ;
String: with a matching string;
Flags: the control flags when the regular expression is used;
Code
2, re.match (pattern, string, flags=0) matches the regular expression from the beginning of a string, returns the match object
3, re.findall (pattern, string, flags=0)
Search for a string that returns all substrings that match.
4, re.split (pattern, string, maxsplit = 0, flags = 0)
Splits a string into regular expression matching results, returning the list type.
Maxsplit: The maximum number of splits, the remainder is output as the last element.
5, re.finditer (pattern, string, flags=0)
Searches for a string, returns an iteration type that matches the result, and each iteration element is a match object.
6, re.sub (pattern, repl, string, count = 0, flags = 0)
Replaces all substrings that match the regular expression in a string, returning the replaced string.
Pattern: a string or a native string representation of a regular expression;
Repl: replace the string that matches the string;
String: the string to be matched;
Count: the maximum number of substitutions matched;
(6) Another use of re library
(7) match object
1, the properties of the match object
2, the method of the match object
(8) Greedy match and minimum match of re library
Re library defaults to greedy matching, which means that the output matches the longest string.
1, the minimum match operator
Brand MIGHTY MAX BATTERY
The battery is composed of nickel -cadmium batteries
Recommended purpose of the product IROBOT
Voltage 14.4 volts
Rechargeable
500-2000Mah Lithium Battery,2000Mah Rechargeable Battery,5000 Mah Power Bank,5000Mah 26650 Battery,Cylindrical Lithium Battery
Langrui Energy (Shenzhen) Co.,Ltd , https://www.langruibattery.com