Nearby lessons
125 of 159Python - Regex Mobile Number with Country Code
- Understand how a single Regular Expression can handle 10-digit, 11-digit and 12-digit mobile-number forms
- Apply the alternation operator | inside the group (0|91)
- Learn how the ? quantifier makes the complete prefix group optional
- Build a complete mobile-number validation program using re.fullmatch()
- Analyse why valid numbers match and invalid numbers fail according to the tutorial pattern
Introduction to Mobile Number with Country Code
In the previous examples, we validated a basic 10-digit mobile number.
Sometimes a mobile number may also be written with:
- Only the 10-digit mobile number
0before the mobile number91before the mobile number
For this tutorial, we use the following Regular Expression:
(0|91)?[7-9][0-9]{9}
This pattern can accept these three forms:
9876543210
09876543210
919876543210
Therefore, the tutorial demonstrates how a single Regular Expression can handle 10-digit, 11-digit and 12-digit forms.
The Regular Expression - Complete Pattern Breakdown
The complete Regular Expression is:
(0|91)?[7-9][0-9]{9}
It can be divided into three major parts:
(0|91)? [7-9] [0-9]{9}
│ │ │
│ │ └── Remaining 9 digits
│ │
│ └── First mobile digit: 7, 8, or 9
│
└── Optional prefix: 0 or 91
| Regex | Meaning |
|---|---|
(0|91) |
Matches either 0 or 91 |
? |
Makes the complete group optional |
[7-9] |
Matches one digit: 7, 8, or 9 |
[0-9] |
Matches one ASCII digit from 0 to 9 |
{9} |
Requires exactly nine occurrences |
Therefore:
(0|91)?[7-9][0-9]{9}
means:
Optionally start with 0 or 91, followed by a 10-digit mobile number whose first digit is 7, 8, or 9.
Understanding (0|91) - The Alternation Operator
The first part is:
(0|91)
Parentheses create a group.
Inside the group:
0|91
the pipe symbol | represents an alternative.
Therefore:
0|91
means:
0
OR
91
So the group can match either:
0
or:
91
The pipe symbol:
|
is called the alternation operator.
It represents an OR condition.
For example:
0|91
means:
0 OR 91
By grouping these alternatives:
(0|91)
and adding:
?
we make the entire prefix optional.
Understanding the ? Quantifier
After the group, we have:
?
The ? quantifier means:
Zero or one occurrence of the preceding pattern.
Therefore:
(0|91)?
means that the prefix can occur zero or one time.
So we have three possibilities:
No prefix
OR
0
OR
91
(0|91)?
│
┌────────┼────────┐
│ │ │
▼ ▼ ▼
No Prefix 0 91
│ │ │
▼ ▼ ▼
9876543210 09876543210 919876543210
The ? applies to the complete group:
(0|91)
not just to the final digit.
The Optional Prefix - 0 and 91
Optional 0
The mobile number may contain an optional:
0
before the 10-digit number.
For example:
09876543210
Breakdown:
0 9 876543210
│ │ │
│ │ └── Remaining 9 digits
│ │
│ └── First mobile digit
│
└── Optional prefix
Total characters:
1 + 10 = 11 digits
Optional 91
Instead of 0, the number may contain:
91
as the optional prefix in this tutorial pattern.
For example:
919876543210
Breakdown:
91 9 876543210
│ │ │
│ │ └── Remaining 9 digits
│ │
│ └── First mobile digit
│
└── Optional 91 prefix
Total characters:
2 + 10 = 12 digits
The Three Valid Forms - 10, 11 and 12-Digit Numbers
10-Digit Number
Because the prefix group is optional, we can completely omit it.
For example:
9876543210
Matching:
(0|91)? → Nothing
[7-9] → 9
[0-9]{9} → 876543210
Therefore:
9876543210
satisfies the tutorial pattern.
11-Digit Number
For an 11-digit number, the optional prefix is:
0
Example:
09876543210
Matching:
(0|91)? → 0
[7-9] → 9
[0-9]{9} → 876543210
Therefore:
09876543210
satisfies the tutorial pattern.
12-Digit Number
For a 12-digit number, the optional prefix is:
91
Example:
919876543210
Matching:
(0|91)? → 91
[7-9] → 9
[0-9]{9} → 876543210
Therefore:
919876543210
satisfies the tutorial pattern.
Comparison of All Three Forms
| Input | Prefix | Mobile Part | Length | Pattern Result |
|---|---|---|---|---|
9876543210 |
No prefix | 9876543210 | 10 | Valid |
09876543210 |
0 | 9876543210 | 11 | Valid |
919876543210 |
91 | 9876543210 | 12 | Valid |
Understanding [7-9] and [0-9]{9}
Understanding [7-9]
After the optional prefix, the pattern contains:
[7-9]
This matches exactly one character from:
7
8
9
Therefore, according to this tutorial rule, the actual 10-digit mobile-number portion must start with:
7, 8, or 9
Examples:
7894561230
8765432109
9876543210
Understanding [0-9]{9}
The remaining part is:
[0-9]{9}
[0-9] matches one ASCII digit from 0 to 9.
The quantifier:
{9}
requires exactly nine occurrences.
Therefore:
[7-9][0-9]{9}
contains:
1 first digit
+
9 remaining digits
──────────────────
10 digits
Complete Pattern Visualization
The complete pattern combines the optional prefix, the first mobile digit, and the remaining digits:
(0|91)?[7-9][0-9]{9}
┌─────────┐
│ (0|91)? │
└────┬────┘
│
├── Nothing
├── 0
└── 91
+
┌───────┐
│ [7-9] │
└───┬───┘
│
└── 7, 8, or 9
+
┌───────────┐
│ [0-9]{9} │
└─────┬─────┘
│
└── Exactly 9 digits
Putting it all together:
(0|91)?
Optional prefix
│
├── Nothing
├── 0
└── 91
+
[7-9]
First mobile digit
│
├── 7
├── 8
└── 9
+
[0-9]{9}
Exactly 9 more digits
│
▼
10-digit number
11-digit number with 0
12-digit number with 91
Complete Program
This program reads a mobile number from the user and validates it against the tutorial Regular Expression using re.fullmatch().
If the complete input matches the pattern, the number is Valid; otherwise it is Invalid.
Program Outputs for All Forms
The same program accepts all three valid forms.
Output 1 - 10-Digit Number
Enter Mobile Number: 9876543210
Valid Mobile Number
Output 2 - 11-Digit Number
Enter Mobile Number: 09876543210
Valid Mobile Number
Output 3 - 12-Digit Number
Enter Mobile Number: 919876543210
Valid Mobile Number
Output 4 - Invalid Number
Enter Mobile Number: 6876543210
Invalid Mobile Number
Complete Program Explanation
Let us understand the complete program step by step.
Step 1 - Import re Module
import re
The re module provides Regular Expression functionality in Python.
Step 2 - Read Mobile Number
mobile_number = input("Enter Mobile Number: ")
The user enters a mobile number, and input() stores it as a string.
Step 3 - Define Regular Expression
pattern = r"(0|91)?[7-9][0-9]{9}"
This pattern contains:
(0|91)? Optional 0 or 91
[7-9] First digit of mobile portion
[0-9]{9} Remaining nine digits
Step 4 - Validate Complete Input
m = re.fullmatch(pattern, mobile_number)
fullmatch() checks whether the entire entered string matches the Regular Expression.
Step 5 - Check Match Result
if m != None:
If matching succeeds, m contains a Match Object.
If matching fails, m contains:
None
Step 6 - Display Result
if m != None:
print("Valid Mobile Number")
else:
print("Invalid Mobile Number")
Why Each Number is Valid or Invalid
Why 9876543210 is Valid
Consider:
9876543210
There is no prefix.
(0|91)? → Nothing ✓
[7-9] → 9 ✓
[0-9]{9} → 876543210 ✓
All parts match.
Result → VALID
Why 09876543210 is Valid
Consider:
09876543210
Breakdown:
0 | 9876543210
↑ ↑
│ └── 10-digit mobile portion
│
└── Optional prefix
Pattern matching:
(0|91)? → 0 ✓
[7-9] → 9 ✓
[0-9]{9} → 876543210 ✓
Therefore:
Result → VALID
Why 919876543210 is Valid
Consider:
919876543210
Breakdown:
91 | 9876543210
↑ ↑
│ └── 10-digit mobile portion
│
└── Optional 91 prefix
Pattern matching:
(0|91)? → 91 ✓
[7-9] → 9 ✓
[0-9]{9} → 876543210 ✓
Therefore:
Result → VALID
Why 6876543210 is Invalid
Consider:
6876543210
The optional prefix is absent.
The first digit of the actual mobile portion is:
6
But the pattern requires:
[7-9]
which allows only:
7
8
9
Therefore:
6 does not match [7-9]
Result → INVALID
More Valid and Invalid Examples
More Valid Examples
| Input | Reason |
|---|---|
9876543210 |
Valid 10-digit form according to the pattern |
8765432109 |
Starts with 8 and has 10 digits |
7894561230 |
Starts with 7 and has 10 digits |
09876543210 |
Optional 0 prefix |
08765432109 |
Optional 0 prefix followed by a valid mobile portion |
919876543210 |
Optional 91 prefix |
918765432109 |
Optional 91 prefix followed by a valid mobile portion |
Invalid Examples
| Input | Reason |
|---|---|
6876543210 |
Mobile portion starts with 6 |
987654321 |
Only 9 digits without a prefix |
98765432101 |
11 digits but does not use the required 0 prefix structure |
929876543210 |
92 is not an allowed prefix |
009876543210 |
The pattern permits only one optional 0 prefix |
+919876543210 |
The plus sign is not included in this tutorial pattern |
91 9876543210 |
Spaces are not included in this tutorial pattern |
91-9876543210 |
Hyphen is not included in this tutorial pattern |
Important - What ? Applies To and the Capturing Group
What ? Applies To
Consider:
(0|91)?
The ? appears after the closing parenthesis.
Therefore, it applies to the entire group:
(0|91)
This means:
Zero occurrences
│
▼
No prefix
OR
One occurrence
│
├── 0
│
└── 91
It does not mean that both prefixes can appear together.
Capturing Group
Parentheses:
(0|91)
create a capturing group.
For validation alone, capturing the prefix is not necessary.
A non-capturing version can also be written as:
(?:0|91)?[7-9][0-9]{9}
Both forms can perform the same validation for this example.
The tutorial uses:
(0|91)?[7-9][0-9]{9}
because it is simpler while learning grouping and alternation.
Why fullmatch() is Used
The program uses:
re.fullmatch(pattern, mobile_number)
because the complete input should satisfy the mobile-number pattern.
For example:
My number is 9876543210
contains a matching number, but the complete string is not itself a mobile number.
Therefore, fullmatch() is suitable for validation.
Entire String Matches?
│
┌───┴───┐
│ │
Yes No
│ │
▼ ▼
Match None
Object
│ │
▼ ▼
VALID INVALID
Explicit ^ and $ anchors are normally unnecessary when fullmatch() is used.
Length Calculation
Without Prefix
[7-9] → 1 digit
[0-9]{9} → 9 digits
──────────────────────
Total → 10 digits
With 0 Prefix
0 → 1 digit
Mobile → 10 digits
──────────────────────
Total → 11 digits
With 91 Prefix
91 → 2 digits
Mobile → 10 digits
──────────────────────
Total → 12 digits
Therefore, the tutorial pattern handles 10-digit, 11-digit and 12-digit forms.
Important Notes - +91, Spaces, Hyphens and Format Validation
Important Note about +91
The tutorial Regular Expression is:
(0|91)?[7-9][0-9]{9}
It accepts:
919876543210
but it does not accept:
+919876543210
because the + character is not included in the pattern.
If a requirement specifically includes the +91 form, the Regular Expression has to be designed accordingly.
Important Note about Spaces and Hyphens
The current pattern does not allow separators such as:
91 9876543210
91-9876543210
98765 43210
The Regular Expression validates only the exact formats defined by:
(0|91)?[7-9][0-9]{9}
Therefore, spaces, hyphens, parentheses and other formatting characters are rejected unless they are explicitly added to the pattern.
Format Validation vs Real Number Verification
A Regular Expression can check whether the entered text follows a required format.
For example:
9876543210
may satisfy the Regular Expression.
However, this does not prove that:
- The mobile number actually exists.
- The SIM is active.
- The number belongs to a particular person.
- The number can receive calls or messages.
Regex Validation
│
▼
Checks Format
Regex Validation
│
╳
▼
Does not verify
actual existence
Quick Revision and Summary
The complete pattern:
(0|91)?[7-9][0-9]{9}
(0|91)?
Optional prefix
│
├── Nothing
├── 0
└── 91
+
[7-9]
First mobile digit
│
├── 7
├── 8
└── 9
+
[0-9]{9}
Exactly 9 more digits
│
▼
10-digit number
11-digit number with 0
12-digit number with 91
│
▼
re.fullmatch()
│
┌──┴──┐
│ │
Match None
│ │
▼ ▼
VALID INVALID
Compact Execution Flow
Mobile Number
│
▼
Check (0|91)?
│
├── Nothing
├── 0
└── 91
│
▼
Check [7-9]
│
├── Fail → INVALID
│
▼
Check [0-9]{9}
│
├── Fail → INVALID
│
▼
Complete Match
│
▼
VALID
Summary Points
- The Regular Expression used in this tutorial is
(0|91)?[7-9][0-9]{9}. (0|91)means either0or91.- The
|symbol represents alternation or OR. - The
?quantifier means zero or one occurrence. - Therefore,
(0|91)?makes the prefix optional. - If the prefix is absent, the pattern accepts a 10-digit mobile-number form.
- If
0is present, the pattern accepts an 11-digit form. - If
91is present, the pattern accepts a 12-digit form. [7-9]requires the actual mobile-number portion to start with 7, 8, or 9.[0-9]{9}requires exactly nine additional ASCII digits.9876543210satisfies the tutorial pattern.09876543210satisfies the tutorial pattern.919876543210satisfies the tutorial pattern.6876543210does not satisfy the pattern because the mobile portion starts with 6.re.fullmatch()checks the complete input.- A successful match returns a Match Object.
- An unsuccessful match returns
None.
- The tutorial pattern (0|91)?[7-9][0-9]{9} accepts a plain 10-digit number, an 11-digit number with a 0 prefix, and a 12-digit number with a 91 prefix
- The pipe symbol | is the alternation operator and represents an OR condition, so (0|91) matches either 0 or 91
- The ? quantifier means zero or one occurrence and makes the entire (0|91) group optional
- [7-9] requires the mobile-number portion to start with 7, 8, or 9, while [0-9]{9} requires exactly nine additional digits
- re.fullmatch() checks whether the entire entered string matches, returning a Match Object on success and None on failure