Open In App

Validating Indian currency data using Regular expressions

Improve
Improve
Like Article
Like
Save
Share
Report

Given some Indian Currency Data, the task is to check if they are valid or not using regular expressions. Rules for valid Indian Currency Data are:

  • Indian Rupee format: The currency string starts with the Indian Rupee symbol ₹, followed by a comma-separated integer part that can have one to three digits, followed by a decimal point, followed by exactly two digits representing the fractional part.
  • Generic format: The currency string starts with an optional Rs. prefix, followed by a comma-separated integer part that can have one to three digits, followed by a decimal point, followed by exactly two digits representing the fractional part.
  • A currency symbol at the beginning can be either “₹” or “Rs.“.
  • A whole number portion that can have 1 to 3 digits, followed by any number of groups of 3 digits separated by commas.
  • A decimal point.
  • Exactly 2 digits in the decimal portion.

Examples:

Input: Rs.12, 34, 567.890
Output: Invalid
Explanation: This currency string has more than two digits in the fractional part, which violates the regular expression pattern that requires exactly two digits in the fractional part. Therefore, this currency string is considered invalid.

Input: Rs.123.456
Output: Invalid
Explanation: This currency string has more than two digits in the fractional part, which violates the regular expression pattern that requires exactly two digits in the fractional part. Therefore, this currency string is considered invalid.

Approach: The problem can be solved based on the following idea:

Create a regex pattern to validate the number as written below:   

regex = ^₹[0-9]{1, 3}(, [0-9]{3})*\\.[0-9]{2}$|^(Rs\\.\\s)?

Where,

  • ^ – The caret symbol matches the beginning of the string. This ensures that the pattern only matches currency values that start at the beginning of the string.
  • ₹ – This matches the Indian rupee currency symbol.
  • [0-9]{1, 3} – This matches 1 to 3 digits.
  • ([0-9]{3})* – This matches any number of groups of 3 digits separated by commas. The * character specifies that the preceding pattern (in this case, (, [0-9]{3})) should be matched zero or more times.
  • \\. – This matches a decimal point. The \\ is an escape sequence that represents a single backslash, and the dot . matches any character.
  • [0-9]{2} – This matches exactly 2 digits.
  • $ – The dollar sign matches the end of the string. This ensures that the pattern only matches currency values that end at the end of the string.
  • | – The pipe symbol is used to separate two alternative patterns. In this case, it separates the pattern for currency values starting with “₹” from the pattern for currency values starting with “Rs.”.
  • (Rs\\.\\s)? – This matches an optional “Rs.” symbol followed by a space character. The? character specifies that the preceding pattern (in this case, (Rs\\.\\s)) should be matched zero or one time.

By combining these symbols and patterns, we can create a regular expression pattern that matches valid Indian currency.

Follow the below steps to implement the idea:

  • Create a regex expression for Indian Currency Data.
  • Use Pattern class to compile the regex formed.
  • Use the matcher function to check whether the Indian Currency Data Number is valid or not.
  • If it is valid, return true. Otherwise, return false.

Below is the code implementation of the above-discussed approach:

C++

// C++ Implementation
#include <iomanip>
#include <iostream>
#include <regex>
#include <string>
using namespace std;
 
// Function to check whether currency is
// valid
void check(string currency, string pattern)
{
 
    regex regex_pattern(pattern);
 
    if (regex_match(currency, regex_pattern)) {
        cout << "Valid" << endl;
    }
    else {
        cout << "Invalid" << endl;
    }
}
 
// Drivers code
int main()
{
 
    string currency = "₹ 1, 23, 456.78";
    string pattern = "^₹[0-9]{1, 3}(, [0-9]{3})*\\.[0-9]{2}$|"
                     "^(Rs\\.\\s)";
 
    // Function call 1
    check(currency, pattern);
 
    currency = "Rs. 123.456";
 
    // Function call 2
    check(currency, pattern);
 
    return 0;
}

                    

Java

// java implementation
import java.util.regex.*;
 
public class GFG {
 
    // Function to check whether currency is valid
    static void check(String currency, String pattern)
    {
        Pattern regexPattern = Pattern.compile(pattern);
 
        if (regexPattern.matcher(currency).matches()) {
            System.out.println("Valid");
        }
        else {
            System.out.println("Invalid");
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        String currency
            = "\u20B9"
              + " 1, 23, 456.78"; // ₹ 1, 23, 456.78
        String pattern
            = "^\\u20B9[0-9]{1,3}(, [0-9]{3})*\\.[0-9]{2}$|^(Rs\\.\\s)";
 
        // Function call 1
        check(currency, pattern);
 
        currency = "Rs. 123.456";
 
        // Function call 2
        check(currency, pattern);
    }
}

                    

Python3

import re
 
# Function to check whether currency is valid
def check(currency, pattern):
    if re.fullmatch(pattern, currency):
        print("Valid")
    else:
        print("Invalid")
 
# Driver code
if __name__ == "__main__":
    currency = "\u20B9 1, 23, 456.78"  # ₹ 1, 23, 456.78
    pattern = r'^\u20B9[0-9]{1,3}(, [0-9]{3})*\.[0-9]{2}$|^(Rs\.\s)'
 
    # Function call 1
    check(currency, pattern)
 
    currency = "Rs. 123.456"
 
    # Function call 2
    check(currency, pattern)

                    

C#

using System;
using System.Text.RegularExpressions;
 
class CurrencyValidator
{
   
    // Function to check whether currency is valid
    static void Check(string currency, string pattern)
    {
        Regex regexPattern = new Regex(pattern);
 
        if (regexPattern.IsMatch(currency)) {
            Console.WriteLine("Valid");
        }
        else {
            Console.WriteLine("Invalid");
        }
    }
 
    // Drivers code
    static void Main()
    {
        string currency = "₹ 1, 23, 456.78";
        string pattern
            = @"^₹[0-9]{1,3}(, [0-9]{3})*\.[0-9]{2}$|^(Rs\.\s)";
 
        // Function call 1
        Check(currency, pattern);
 
        currency = "Rs. 123.456";
 
        // Function call 2
        Check(currency, pattern);
    }
}

                    

Javascript

// JavaScript Implementation
 
// Function to check whether currency is valid
function check(currency, pattern) {
    const regexPattern = new RegExp(pattern);
 
    if (regexPattern.test(currency)) {
        console.log("Valid");
    } else {
        console.log("Invalid");
    }
}
 
// Drivers code
function main() {
    let currency = "₹ 1, 23, 456.78";
    let pattern = /^₹[0-9]{1,3}(, [0-9]{3})*\.[0-9]{2}$|^(Rs\.\s)/;
 
    // Function call 1
    check(currency, pattern);
 
    currency = "Rs. 123.456";
 
    // Function call 2
    check(currency, pattern);
}
 
// Invoke the main function
main();

                    

Output
Invalid
Invalid





Time complexity: O(n), where n is the length of the string.
Auxiliary space: O(1)



Last Updated : 09 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads