1 Front phone number finder can work, but use a lot of code, do things Limited;
The Isphonenumber () function has 17 rows, but only one phone number pattern can be found. Like 415.555.4242 or (415) 555-4242 Such a phone number format, how to check?
If the phone number has an extension, such as 415-555-4242 x99, how to check?
So the Isphonenumber () function fails at the time of validation.
2 regular expressions, referred to as regex, are the descriptive methods of text patterns.
For example, \d is a regular expression that represents a numeric character, that is, any number from 0 to 9.
Python uses regular expression \d\d\d-\d\d\d-\d\d\d to match the same text that preceded the Isphonenumber () function: 3 digits, a dash, 3 digits, a dash, 4 digits
All other strings do not match the \d\d\d-\d\d\d-\d\d\d regular expression
But regular expressions can be much more complex.
For example: 3 ({3}) enclosed in curly braces after a pattern, that is, "match this pattern 3 times". So a shorter regular expression \d{3}-\d{3}-\d{4} can also match the correct phone number format.
The functions of all regular expressions in the Python are in the RE module. Enter the code in the interactive environment, import the module
Import re
Note: In the actual development example, you need to use the RE module, so remember to import it at the beginning of each script, or restart idle.
Otherwise, error message Nameerror:name ' re ' is not defined
passes a string value to Re.compile (), representing the regular expression, It returns a Regex pattern object (or simply a Regex object)
to create a Regex object to match the phone number pattern, enter the following code in the interactive environment:
Recall the following, \d means "a numeric character", \d\d\d-\d\d\ D-\d\d\d\d is the regular expression for the correct phone number pattern.
Phonenumregex = re.compile (R ' \d\d\d-\d\d\d-\d\d\d\d ')
now contains a Regex object for the Phonenumregex variable
The search () method of the Regex object looks for the passed-in string, looking for all matches of the regular expression.
If the regular expression pattern is not found in the string, the search () method returns none. If the pattern is found, the search () method returns a match.
Match has a group () method that returns the text that is actually matched in the found string,
For example:
Phonenumregex = Re.compile (R ' \d\d\d-\d\d\d-\d\d\d\d ')
Mo = Phonenumregex.search (' My number is 415-555-4242. ')
Print (' Phone number found: ' + mo.group)
variable name mo is a generic name for the match object. This example may seem a little complicated, but it is much shorter than the previous isphonenumber.py program and does the same thing.
Steps for the above code:
1 Pass the expected pattern to Re.compile () and save the resulting Regex object in Phonenumregex.
2 on Phonenumregex, use Search () to pass in the string you want to find
3 The result of the lookup is saved in the variable mo
In this example,
1 knows that the pattern is found in this string, so know that it will return a match pair Like.
2 knows that Mo contains a match object instead of a null value, you can call group () on the MO variable to return the matching result.
3 writes Mo.group () in the print statement, showing a complete match, which is 415-555-4242
Steps for Python to use regular expressions:
1 importing regular expressions with import re
2 Create a Regex object with the Re.compile () function (remember to use the original string)
3 passes the search () method of the Regex object to the string you want to find. It will return a match object
4 Call the group () method of the match object to return the string that actually matches the text
A regular expression test program that can take advantage of Web pages to clearly indicate how a regular expression matches a piece of text entered
Recommended test procedures are located in http://regexpal.com/
Regular introduction, creating, matching regex objects