US currency validation using Regular Expressions
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: True
Input: “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++
#include <bits/stdc++.h>
#include <regex>
using namespace std;
string isValid_USCurrency(string str)
{
const regex pattern(
"^\\$(\\d{1, 3}(\\, \\d{3})*|(\\d+))(\\.\\d{2})?$" );
if (str.empty()) {
return "false" ;
}
if (regex_match(str, pattern)) {
return "true" ;
}
else {
return "false" ;
}
}
int main()
{
string str1 = "$123458" ;
cout << isValid_USCurrency(str1) << endl;
string str2 = "$1, 234, 567.89" ;
cout << isValid_USCurrency(str2) << endl;
string str3 = "$0.84" ;
cout << isValid_USCurrency(str3) << endl;
string str4 = "$12, 3456.01" ;
cout << isValid_USCurrency(str4) << endl;
string str5 = "$1.234" ;
cout << isValid_USCurrency(str5) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isValid_USCurrency(String str)
{
String regex
= "^\\$(\\d{1, 3}(\\, \\d{3})*|(\\d+))(\\.\\d{2})?$" ;
Pattern p = Pattern.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "$123458" ;
System.out.println(isValid_USCurrency(str1));
String str2 = "$1, 234, 567.89" ;
System.out.println(isValid_USCurrency(str2));
String str3 = "$0.84" ;
System.out.println(isValid_USCurrency(str3));
String str4 = "$12, 3456.01" ;
System.out.println(isValid_USCurrency(str4));
String str5 = "$1.234" ;
System.out.println(isValid_USCurrency(str5));
}
}
|
Python3
import re
def isValid_USCurrency( str ):
regex = "^\\$(\\d{1, 3}(\\, \\d{3})*|(\\d+))(\\.\\d{2})?$"
p = re. compile (regex)
if ( str = = None ):
return "false"
if (re.search(p, str )):
return "true"
else :
return "false"
if __name__ = = '__main__' :
str1 = "$123458"
print (isValid_USCurrency(str1))
str2 = "$1, 234, 567.89"
print (isValid_USCurrency(str2))
str3 = "$0.84"
print (isValid_USCurrency(str3))
str4 = "$12, 3456.01"
print (isValid_USCurrency(str4))
str5 = "$1.234"
print (isValid_USCurrency(str5))
|
C#
using System;
using System.Text.RegularExpressions;
public class GFG {
public static bool isValid_USCurrency( string str)
{
string regex
= "^\\$(\\d{1, 3}(\\, \\d{3})*|(\\d+))(\\.\\d{2})?$" ;
Regex p = new Regex(regex);
if (str == null ) {
return false ;
}
Match m = p.Match(str);
return m.Success;
}
public static void Main()
{
string str1 = "$123458" ;
Console.WriteLine(isValid_USCurrency(str1));
string str2 = "$1, 234, 567.89" ;
Console.WriteLine(isValid_USCurrency(str2));
string str3 = "$0.84" ;
Console.WriteLine(isValid_USCurrency(str3));
string str4 = "$12, 3456.01" ;
Console.WriteLine(isValid_USCurrency(str4));
string str5 = "$1.234" ;
Console.WriteLine(isValid_USCurrency(str5));
}
}
|
Javascript
function isValid_USCurrency(str) {
let regex = new RegExp(/^\$(\d{1, 3}(\, \d{3})*|(\d+))(\.\d{2})?$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "$123458" ;
console.log(isValid_USCurrency(str1));
let str2 = "$1, 234, 567.89" ;
console.log(isValid_USCurrency(str2));
let str3 = "$0.84" ;
console.log(isValid_USCurrency(str3));
let str4 = "$12, 3456.01" ;
console.log(isValid_USCurrency(str4));
let str5 = "$1.234" ;
console.log(isValid_USCurrency(str5));
|
Output
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:
Last Updated :
16 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...