
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a Python Regular Expression to Validate Numbers
Here, we will learn how to write a regular expression for validating a number in Python. As you may know Python has a module called re module for working with regular expression easily using the built-in functions. The re.match() function matches or validate the regex pattern with the given number.
There are several ways to validate a number using regular expression, such as -
Example: Validate Integer Number
The following example will use different integer values to validate that they are numbers or not. We are using r"^[+-]?\d+$" pattern to validate whether a given string is an integer value.
The pattern ^[+-]? and \d will help us to match one or more digits with + or - signs. And re.match() will help us to match this pattern.
# Import re module import re # define different numbers txt1 = "123" txt2 = "-456" txt3 = "123nick" # define regex pattern pat = r"^[+-]?\d+$" # create a function to match a number def is_match(txt): # make sure txt is a string return bool(re.match(pat, str(txt))) # Print the result print(is_match(txt1)) print(is_match(txt2)) print(is_match(txt3))
Output
This will create the below outcome -
True True False
Example: Validate Real Number
This is another easy way to check if a given number is a real number or not. Here the pattern we have used is r"^[+-]?\d+(\.\d+)?$", in which pattern ^[+-]? and \d will work same as the above example but the pattern \. will look for a decimal point followed by digits.
# Import re module import re # define different numbers txt1 = "3.14" txt2 = "-2.71" txt3 = "123" txt4 = "abc" # define regex pattern pat = r"^[+-]?\d+(\.\d+)?$" # create a function to match a number def is_match(txt): # make sure txt is a string return bool(re.match(pat, str(txt))) # print the result print(is_match(txt1)) print(is_match(txt2)) print(is_match(txt3)) print(is_match(txt4))
Output
This will generate the below result -
True True True False
Example: Validate Comma Separated Numbers
In the below program we are using the r"^[+-]?(\d{1,3}(,\d{3})*+)(\.\d+)?$" pattern to check that the given string shows a comma separated numbers or not.
The pattern (\d{1,3}(,\d{3})*|\d+) is used to make sure numbers are present with commas like "1,234" or "1,234.56".
# Import re module import re # define comma separated numbers txt1 = "5,456.98" txt2 = "236235632" txt3 = "-2,568" txt4 = "34,24" # define regex pattern pat = r"^[+-]?(\d{1,3}(,\d{3})*+)(\.\d+)?$" # create a function to match a number def is_match(txt): # make sure txt is a string return bool(re.match(pat, str(txt))) # print the result print(is_match(txt1)) print(is_match(txt2)) print(is_match(txt3)) print(is_match(txt4))
Output
This will produce the following result -
True True True False