Given some US Currencies, the duty is to examine if they’re legitimate or not utilizing common expressions. Guidelines for the legitimate Foreign money are:
- It ought to at all times begin from “$“.
- It might probably include solely digits (0 – 9) and at most one dot.
- It shouldn’t include any whitespaces and alphabets.
- Comma Separator (‘, ‘) needs to be there after each three digits interval.
Examples:
Enter: “$0.84”
Output: True
Enter: “12345”
Output: False
Clarification: “$” is lacking within the beginning.
Method: The issue could be solved primarily based on the next thought:
Create a regex sample to validate the quantity as written beneath:
regex = “^$([0-9]{1, 3}(, [0-9]{3})*|([0-9]+))(.[0-9]{2})?$”
OR
regex=”^$(d{1, 3}(, d{3})*|(d+))(.d{2})?$“
The place,
- ^ : Represents, starting of the string
- $ : Ought to at all times begin from $
- d : Digits needs to be there
- . : dot could be current or not
- d{2} : Solely digits are allowed after dot
Observe the beneath steps to implement the concept:
- Create a regex expression for US Foreign money.
- Use Sample class to compile the regex shaped.
- Use the matcher perform to examine whether or not the US Foreign money is legitimate or not.
- Whether it is legitimate, return true. In any other case, return false.
Under is the implementation of the above method.
C++
#embody <bits/stdc++.h>
#embody <regex>
utilizing namespace std;
string isValid_USCurrency(string str)
{
const regex sample(
"^$(d{1, 3}(, d{3})*|(d+))(.d{2})?$" );
if (str.empty()) {
return "false" ;
}
if (regex_match(str, sample)) {
return "true" ;
}
else {
return "false" ;
}
}
int essential()
{
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})?$" ;
Sample p = Sample.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void essential(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))
|
Javascript
perform isValid_USCurrency(str) {
let regex = new RegExp(/^$(d{1, 3}(, d{3})*|(d+))(.d{2})?$/);
if (str == null ) {
return "false" ;
}
if (regex.check(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
Associated Articles: