
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to write a Python Regular Expression to validate numbers?
The following code validates a number exactly equal to '2018'
Example
import re s = '2018' match = re.match(r'\b2018\b',s) print match.group()
Output
This gives the output
2018
Example
The following code validates any five digit positive integer
import re s = '2346' match = re.match(r'(?<!-)\b[1-9]\d{4}\b',s) print match s2 = '56789' match = re.match(r'(?<!-)\b[1-9]\d{4}\b',s2) print match.group()
Output
None 56789
- Related Articles
- How to write a Python regular expression to get numbers except decimal?
- How to write a Python regular expression that matches floating point numbers?
- How to validate an email id using regular expression in Python?
- How to write a Python regular expression to use re.findall()?
- How to validate a URL using regular expression in C#?
- How do you validate a URL with a regular expression in Python?
- How to write Python regular expression to check alphanumeric characters?
- How to write a Python regular expression to match multiple words anywhere?
- How to write a case insensitive Python regular expression without re.compile?
- How to write Python regular expression to match with file extension?
- How to extract numbers from text using Python regular expression?
- How to write Python Regular Expression find repeating digits in a number?
- How to write a regular expression to match either a or b in Python?
- How to write a Regular Expression in JavaScript to remove spaces?
- How to write Python regular expression to get all the anchor tags in a webpage?

Advertisements