Regular expressions
Strings are the most data structure involved in programming, and the need to manipulate strings is almost ubiquitous. For example, to determine whether a string is a legitimate email address, although it can be programmed to extract the substring before and after, and then determine whether it is coarse and the domain name, but this is not only cumbersome, and the code is difficult to reuse.
A regular expression is a powerful weapon used to match strings. Its design idea is to use a descriptive language to define a rule for a string, and any string that conforms to the rule, we think it "matches", otherwise the string is illegal.
So the way we judge whether a string is a legitimate email is:
1. Create a regular expression that matches the email;
2, use the regular expression to match the user's input to determine whether it is legal
Because regular expressions are also represented by strings, we first need to know how to use characters to describe a character
Special characters
Variable-length characters
Advanced
Re module
Since the Python string itself is also escaped with \, pay special attention to: s= ' ASDF\\ASDF '
Therefore, we strongly recommend that you use the Python R prefix without considering escaping the problem.
function Re.match(Re.match tries to match a pattern from the starting position of the string, and if the match is not successful, match () returns none)
Re.match (Pattern, string, flags=0)
The function Re.search scans the entire string and returns the first successful match, otherwise none is returned (Re.search (pattern, String, flags=0))
function Re.findall returns all matched strings as a list (Re.findall (pattern, string[, flags]))
extracting substrings In addition to simply determining whether or not to match, regular expressions also have the powerful function of extracting substrings. The grouping to extract is represented by ()
^ (\d{3}-(\d{3,8}$)
two groups are defined, and the area code and native number can be extracted directly from the substring .
Greedy match
A regular match is a greedy match by default, which is to match as many characters as possible.
Replace
Python's Re module provides re.sub to replace matches in a string (Re.sub (Pattern, Repl, String, max=0))
Modifiers for regular expressions
Python Regular Expressions