Python and regular expressions
Using regular expressions with Python is easy. There is a built-in library called
re which provides all of the functionality we will require. There are a lot of functions in this library so we will be only looking at a small subset:
If you are interested in investigating the full range of functionality available in the
re library then the Python documentation is waiting for you.
Post code validation
In the previous section we created a regular expression for validating U.K. post codes. If you managed to develop the full single expression to validate all six types of post code it might look like this:
Let's see how we could turn this into a program which would tell us whether we had entered a valid post code or not.
Use the above video to create validation programs for your regular expressions for telephone numbers and car registrations.
Time to get verbose
Reading regular expressions can be a bit of a nightmare, especially if you are not used to it and your are trying to focus on Python code at the same time. Thankfully there is another option available to us - verbose regular expressions. Verbose expressions in Python can contain comments so they are much more readable.
Use of verbose expressions should be encouraged, especially whilst learning. They are far more readable than compact expressions. However, you should be familiar with both.
Use the above video to convert your telephone numbers and car registrations programs from Task 5 to verbose regular expressions.
The other functions mentioned
re.sub() all work in a similar way to the
re.match() function. The table below indicates how they differ:
|Function||Required Parameters||Optional Parameters||Return Value||Example||Explanation|
||a list containing all occurrences of the pattern||
||pattern, replacement text, string||
||string with replacements||
Create a program that makes use of the following regular expressions functions
Time to move on
Over the past few pages we have looked at the purpose of regular expressions, been introduced to the syntax and the differences between compact and verbose expressions. Now that we have used them in Python it is time to look at how we can use regular expressions in conjunction with server-side scripting to help validate forms.