Given some US Currencies, the task is to check if they are valid or not using regular expressions. Rules for the valid Currency are:
- It should always start with “$“.
- It can contain only digits (0 – 9) and at most one dot.
- It should not contain any whitespaces and alphabets.
- Comma Separator (‘, ‘) should be there after every three digits interval.
Examples:
Input: “$0.84”
Output: TrueInput: “12345”
Output: False
Explanation: “$” is missing in the starting.
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]+))(\.[0-9]{2})?$”OR
regex=”^\$(\d{1, 3}(\, \d{3})*|(\d+))(\.\d{2})?$“
Where,
- ^ : Represents, beginning of the string
- \$ : Should always start from $
- \d : Digits should be there
- \. : dot can be present or not
- \d{2} : Only digits are allowed after dot
Follow the below steps to implement the idea:
- Create a regex expression for US Currency.
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the US Currency is valid or not.
- If it is valid, return true. Otherwise, return false.
Below is the implementation of the above approach.
C++
// C++ program to validate the // US CURRENCY using Regular // Expression #include <bits/stdc++.h> #include <regex> using namespace std; // Function to validate the // US CURRENCY string isValid_USCurrency(string str) { // Regex to check valid // US CURRENCY. const regex pattern( "^\\$(\\d{1, 3}(\\, \\d{3})*|(\\d+))(\\.\\d{2})?$" ); // If the str // is empty return false if (str.empty()) { return "false" ; } // Return true if the str // matched the ReGex if (regex_match(str, pattern)) { return "true" ; } else { return "false" ; } } // Driver Code int main() { // Test Case 1: string str1 = "$123458" ; cout << isValid_USCurrency(str1) << endl; // Test Case 2: string str2 = "$1, 234, 567.89" ; cout << isValid_USCurrency(str2) << endl; // Test Case 3: string str3 = "$0.84" ; cout << isValid_USCurrency(str3) << endl; // Test Case 4: string str4 = "$12, 3456.01" ; cout << isValid_USCurrency(str4) << endl; // Test Case 5: string str5 = "$1.234" ; cout << isValid_USCurrency(str5) << endl; return 0; } |
Java
// Java program to validate the // US Currency using Regular Expression import java.util.regex.*; class GFG { // Function to validate the // US Currency public static boolean isValid_USCurrency(String str) { // Regex to check valid US Currency String regex = "^\\$(\\d{1, 3}(\\, \\d{3})*|(\\d+))(\\.\\d{2})?$" ; // Compile the ReGex Pattern p = Pattern.compile(regex); // If the str is empty return false if (str == null ) { return false ; } // Pattern class contains matcher() // method to find matching between // given str using regex. Matcher m = p.matcher(str); // Return if the str // matched the ReGex return m.matches(); } // Driver Code. public static void main(String args[]) { // Test Case 1: String str1 = "$123458" ; System.out.println(isValid_USCurrency(str1)); // Test Case 2: String str2 = "$1, 234, 567.89" ; System.out.println(isValid_USCurrency(str2)); // Test Case 3: String str3 = "$0.84" ; System.out.println(isValid_USCurrency(str3)); // Test Case 4: String str4 = "$12, 3456.01" ; System.out.println(isValid_USCurrency(str4)); // Test Case 5: String str5 = "$1.234" ; System.out.println(isValid_USCurrency(str5)); } } |
Python3
# Python3 program to validate # US Currency using Regular Expression import re # Function to validate # US Currency def isValid_USCurrency( str ): # Regex to check valid US Currency regex = "^\\$(\\d{1, 3}(\\, \\d{3})*|(\\d+))(\\.\\d{2})?$" # Compile the ReGex p = re. compile (regex) # If the string is empty # return false if ( str = = None ): return "false" # Return if the string # matched the ReGex if (re.search(p, str )): return "true" else : return "false" # Driver code if __name__ = = '__main__' : # Test Case 1: str1 = "$123458" print (isValid_USCurrency(str1)) # Test Case 2: str2 = "$1, 234, 567.89" print (isValid_USCurrency(str2)) # Test Case 3: str3 = "$0.84" print (isValid_USCurrency(str3)) # Test Case 4: str4 = "$12, 3456.01" print (isValid_USCurrency(str4)) # Test Case 5: str5 = "$1.234" print (isValid_USCurrency(str5)) |
C#
// C# program to validate the // US Currency using Regular Expression using System; using System.Text.RegularExpressions; public class GFG { // Function to validate the // US Currency public static bool isValid_USCurrency( string str) { // Regex to check valid US Currency string regex = "^\\$(\\d{1, 3}(\\, \\d{3})*|(\\d+))(\\.\\d{2})?$" ; // Compile the ReGex Regex p = new Regex(regex); // If the str is empty return false if (str == null ) { return false ; } // Pattern class contains matcher() // method to find matching between // given str using regex. Match m = p.Match(str); // Return if the str // matched the ReGex return m.Success; } // Driver Code. public static void Main() { // Test Case 1: string str1 = "$123458" ; Console.WriteLine(isValid_USCurrency(str1)); // Test Case 2: string str2 = "$1, 234, 567.89" ; Console.WriteLine(isValid_USCurrency(str2)); // Test Case 3: string str3 = "$0.84" ; Console.WriteLine(isValid_USCurrency(str3)); // Test Case 4: string str4 = "$12, 3456.01" ; Console.WriteLine(isValid_USCurrency(str4)); // Test Case 5: string str5 = "$1.234" ; Console.WriteLine(isValid_USCurrency(str5)); } } // This code is contributed by Pushpesh Raj. |
Javascript
// Javascript program to validate // US Currency using Regular Expression // Function to validate the // US Currency function isValid_USCurrency(str) { // Regex to check valid // US Currency let regex = new RegExp(/^\$(\d{1, 3}(\, \d{3})*|(\d+))(\.\d{2})?$/); // if str // is empty return false if (str == null ) { return "false" ; } // Return true if the str // matched the ReGex if (regex.test(str) == true ) { return "true" ; } else { return "false" ; } } // Driver Code // Test Case 1: let str1 = "$123458" ; console.log(isValid_USCurrency(str1)); // Test Case 2: let str2 = "$1, 234, 567.89" ; console.log(isValid_USCurrency(str2)); // Test Case 3: let str3 = "$0.84" ; console.log(isValid_USCurrency(str3)); // Test Case 4: let str4 = "$12, 3456.01" ; console.log(isValid_USCurrency(str4)); // Test Case 5: let str5 = "$1.234" ; console.log(isValid_USCurrency(str5)); |
true true true false false
Time Complexity: O(N) for each test case, where N is the length of the given string.
Auxiliary Space: O(1)
Related Articles:
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!