python Regular Expressionsis a special sequence of characters that can help you conveniently check whether a string matches a pattern, and this article will
python Regular ExpressionTell you
Regular ExpressionsThere is a lot of use in Python because he can make arbitrary matches that match the information we want to extract. When we touch
Python regularYou will know the power of the regular. There is a library re in some projects we often call the regular library to do matching related problems.
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 judge whether it is a word and 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 the regular expression is also represented by a string, we first know how to describe the character with characters.
In regular expressions, if a character is given directly, it is exactly the exact match. With \d you can match a number, \w can match a letter or a number, so:
• ' 00\d ' can match ' 007 ', but cannot match ' 00A ';
• ' \d\d\d ' can match ' 010 ';
• ' \w\w\d ' can match ' py3 ';
.
Can match any character, so:
• ' py. ' Can match ' pya ', ' pyb ', ' py! ' and so on.
To match a variable-length character, in a regular expression, use * to represent any character (including 0), with + for at least one character, with the? Represents 0 or 1 characters, with {n} representing n characters, with {n,m} representing n-m characters:
Take a look at a complex example: \d{3}\s+\d{3,8}.
Let's read from left to right:
1. \d{3} indicates a match of 3 digits, e.g. ' 010 ';
2. \s can match a space (also including tab and other whitespace characters), so \s+ indicates at least one space, such as matching "," and so on;
3. \d{3,8} represents 3-8 digits, such as ' 1234567 '.
Together, the above regular expression can match a telephone number with an area code separated by any space.