Overview
A regular expression is a special sequence of characters that is often used to check if it matches a pattern. The eighth lesson mainly introduces the usage of Python regular expressions from the following aspects.
(1) Use of re module
(2) Character matching, quantity representation, boundary representation
(3) Advanced usage of regular expressions
(4) Greedy and non-greedy modes
re module
(1) match (regular expression, string to be matched)
(1) Match items from left to right
(2) For regular matching check, if the "string to be matched" can match the "regular expression", then The match method returns the match object, otherwise it returns None
(2) group() method: used to return the matching part of the string
Character matching, quantity representation, boundary representation
(1) Single character matching
(2) Quantity representation
(3) Boundary representation
(1) String and word boundary
(2) Matching grouping
Advanced usage of regular expressions
Greedy and non-greedy mode
homework questions
Optimization of the milk tea house price settlement system:
1. Use regular expressions to determine whether the mobile phone number entered by the customer complies with the mobile phone number setting rules:
1) Begin with a number 1
2) The second digit is any number in 3578
3) The remaining 9 digits are 0 -9 Any number
2. Output the operator of the mobile phone number. Judgment rules:
Mobile operator: the first three digits of 178 , 182, 183, 184, 187, 188
Unicom operators: The first three mobile phone numbers are 130, 131, 132, 155, 156, 176, 185, 186
Telecom operators: The first three mobile phone numbers are 133, 153, 173, 177, 180, 181, 189, 199
3. Use regular expressions to determine the input milk tea number. If it is not within the range of 1-5, output: Woops! We only sell the above five milk teas! Looking forward to new flavors! And set the judgment process as a method in the class.