Nearby lessons
122 of 159Python - Regular Expressions
- Understand what a regular expression is
- Know why regular expressions are needed for pattern matching
- Identify the main application areas of regular expressions
- Recognize the terminology used with regular expressions
- Import Python's re module for regular expression operations
What are Regular Expressions?
Regular Expressions are one of the important concepts used for pattern matching in strings.
Suppose we want to represent a group of strings that follow a particular format.
Examples:
- Mobile numbers
- Email IDs
- Identifiers
- Vehicle registration numbers
Instead of checking every possible string manually, we can define a pattern using a Regular Expression.
String Data
│
▼
Regular Expression Pattern
│
▼
Pattern Matching
│
▼
Matched / Not Matched
Definition of Regular Expressions
If we want to represent a group of Strings according to a particular format or pattern, then we should go for Regular Expressions.
A Regular Expression is a declarative mechanism used to represent a group of Strings according to a particular format or pattern.
Regular Expression
=
Pattern used to represent
a group of Strings
For example, if we want to represent all valid mobile numbers, we can create one Regular Expression describing the required mobile-number format.
Understanding the Definition
Consider the following values:
9898989898
8989898989
7897897897
These strings have a common format:
- They contain digits.
- They contain a fixed number of digits.
- They can be represented using a common pattern.
A Regular Expression allows us to describe such a pattern instead of checking each value individually.
Many Strings
│
│
▼
Common Format / Pattern
│
▼
Regular Expression
Why Do We Need Regular Expressions?
In real applications, we frequently need to check whether some text follows a required format.
For example:
- Is the entered mobile number valid?
- Is the entered email ID valid?
- Does the text contain a particular pattern?
- Does a string start or end with a particular value?
- Does a file contain mobile numbers?
Regular Expressions provide a convenient mechanism for performing these kinds of pattern-based operations.
Input String
│
▼
Apply Regular Expression
│
▼
Does Pattern Match?
│
┌───┴───┐
▼ ▼
Yes No
Examples of Regular Expressions
Two basic examples help us understand where Regular Expressions can be used.
Example 1 — Mobile Numbers
We can write a Regular Expression to represent all mobile numbers.
Mobile Numbers
│
▼
Required Format
│
▼
Regular Expression
Instead of writing separate validation logic for every possible mobile number, we can define a pattern that represents valid mobile numbers.
Example 2 — Mail IDs
We can write a Regular Expression to represent all mail IDs.
Mail IDs
│
▼
Required Email Format
│
▼
Regular Expression
The Regular Expression describes the required structure of the mail ID.
Regular Expression Concept
The basic concept can be understood using the following flow:
Requirement
│
▼
Identify Format
│
▼
Create Pattern
│
▼
Regular Expression
│
▼
Apply Pattern to Strings
│
▼
Find / Validate / Extract Data
Therefore, Regular Expressions are not limited only to validation. They can also be used for searching and extracting required patterns from text.
Applications of Regular Expressions
The main application areas of Regular Expressions are:
- To develop validation frameworks / validation logic
- To develop Pattern Matching Applications
- To develop Translators like Compilers and Interpreters
- To develop Digital Circuits
- To develop Communication Protocols like TCP/IP and UDP
| Application | Examples / Purpose |
|---|---|
| Validation | Develop validation frameworks and validation logic |
| Pattern Matching | Ctrl + F in Windows, grep in UNIX |
| Translators | Compilers and Interpreters |
| Digital Circuits | Development of Digital Circuits |
| Communication Protocols | TCP/IP and UDP |
Validation means checking whether the given data follows the required format, for example mobile number validation, email ID validation, identifier validation, and vehicle registration number validation.
Pattern matching means searching for required text or patterns inside larger text. Regular Expressions help represent and recognize patterns in source code when building compilers and interpreters.
Python Module for Regular Expressions
Python provides the re module for developing Regular Expression based applications.
We can import the module using:
import re
The
remodule is Python's module for working with Regular Expressions.
Python Program
│
▼
import re
│
▼
Regular Expression Operations
What Can We Do After Importing re?
The statement import re imports Python's Regular Expression module.
The re module contains several built-in functions that make it easy to use Regular Expressions in Python applications.
After importing the module, we can perform operations such as:
- Compiling patterns
- Searching patterns
- Finding matches
- Replacing matched text
- Splitting strings based on patterns
These operations are explained in detail in the upcoming parts of the tutorial.
Basic Regular Expression Workflow in Python
The general idea of working with Regular Expressions in Python is:
Start
│
▼
Import re Module
│
▼
Define Regular Expression Pattern
│
▼
Provide Target String
│
▼
Apply re Module Function
│
▼
Search / Match Pattern
│
▼
Process Result
The exact function used depends on what operation we want to perform.
Regular Expression Terminology
| Term | Meaning |
|---|---|
| Regular Expression | A pattern used to represent a group of Strings |
| Pattern | The format we want to search or match |
| Target String | The string on which the Regular Expression is applied |
| Match | A portion of the target string that satisfies the pattern |
re |
Python module used for Regular Expressions |
Complete Concept Diagram
Regular Expressions
│
▼
Represent Strings by Pattern
│
┌──────────────┼──────────────┐
│ │ │
▼ ▼ ▼
Validation Searching Matching
│
▼
Applications
│
├── Validation Frameworks
├── Pattern Matching
├── Compilers
├── Interpreters
├── Digital Circuits
└── Communication Protocols
│
▼
Python Support
│
▼
re Module
│
▼
import re
The next important concept is compiling a Regular Expression pattern using the compile() function.
- A regular expression represents a group of strings that follow a particular format
- Regular expressions enable validation, searching, matching, and extracting of text
- Mobile numbers, email IDs, identifiers, and vehicle numbers are common use cases
- Applications include validation, pattern matching, compilers, digital circuits, and protocols
- Python's re module is imported with import re and provides built-in regex functions