Validating UPI IDs using Regular Expressions
Given some UPI IDs, the task is to check if they are valid or not using regular expressions. Rules for the valid UPI ID:
- UPI ID is an alphanumeric String i.e., formed using digits(0-9), alphabets (A-Z and a-z), and other special characters.
- It must contain ‘@’.
- It should not contain whitespace.
- It may or may not contain a dot (.) or hyphen (-).
UPI stands for Unified Payments Interface (UPI).UPI IDs are unique IDs given to each customer.
Examples :
Input: str = ”9136812895@ybl?
Output: True
Input: str = ”MH05DL9023 ”
Output: false
Explanation: It does not contain the “@” symbol.
Input: str = ”ViratKohli101@paytm?
Output: true
Input: str =”rahul.12chauhan1998-1@okicici?
Output: true
Input: str = ”1234567890@upi123456?
Output: true
Input: str = ”Akanksha @ybl ”
Output: false
Explanation: It contains a whitespace.
Approach: The problem can be solved based on the following idea:
Create a regex pattern to validate the number as written below:
regex=”^[a-zA-Z0-9.-]{2, 256}@[a-zA-Z][a-zA-Z]{2, 64}$”
^ : Beginning of the string.
[] Character set :Match any character in the set.
[a-zA-Z0-9.-] : Match any character in the range a-z, A-Z, 0-9, hyphen(-), dot(.).
{2, 256} Quantifier: Match between 2 and 256 of the preceding items.
$: End of the string
Follow the below steps to implement the idea:
- Create regex expression for UPI ID.
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the UPI id 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 isValidUpi(string upi_Id)
{
const regex pattern(
"^[a-zA-Z0-9.-]{2, 256}@[a-zA-Z][a-zA-Z]{2, 64}$" );
if (upi_Id.empty()) {
return "false" ;
}
if (regex_match(upi_Id, pattern)) {
return "true" ;
}
else {
return "false" ;
}
}
int main()
{
string str1 = "9136812895@ybl" ;
cout << isValidUpi(str1) << endl;
string str2 = "rahul.12chauhan1998-1@okicici" ;
cout << isValidUpi(str2) << endl;
string str3 = "BNZAA2318JM" ;
cout << isValidUpi(str3) << endl;
string str4 = "934517865" ;
cout << isValidUpi(str4) << endl;
string str5 = "ViratKohli101@paytm" ;
cout << isValidUpi(str5) << endl;
string str6 = "Akanksha @ybl" ;
cout << isValidUpi(str6) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isValidUpi(String upi_Id)
{
String regex
= "^[a-zA-Z0-9.-]{2, 256}@[a-zA-Z][a-zA-Z]{2, 64}$" ;
Pattern p = Pattern.compile(regex);
if (upi_Id == null ) {
return false ;
}
Matcher m = p.matcher(upi_Id);
return m.matches();
}
public static void main(String args[])
{
String str1 = "9136812895@ybl" ;
System.out.println(isValidUpi(str1));
String str2 = "rahul.12chauhan1998-1@okicici" ;
System.out.println(isValidUpi(str2));
String str3 = "BNZAA2318JM" ;
System.out.println(isValidUpi(str3));
String str4 = "934517865" ;
System.out.println(isValidUpi(str4));
String str5 = "ViratKohli101@paytm" ;
System.out.println(isValidUpi(str5));
String str6 = "Akanksha @ybl" ;
System.out.println(isValidUpi(str6));
}
}
|
Python3
import re
def isValidUpi( str ):
regex = "^[a-zA-Z0-9.-]{2, 256}@[a-zA-Z][a-zA-Z]{2, 64}$"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
if __name__ = = '__main__' :
str1 = "9136812895@ybl"
print (isValidUpi(str1))
str2 = "rahul.12chauhan1998-1@okicici"
print (isValidUpi(str2))
str3 = "Rahul 1998"
print (isValidUpi(str3))
str4 = "934517865"
print (isValidUpi(str4))
str5 = "ViratKohli101@paytm"
print (isValidUpi(str5))
str6 = "Akanksha @ybl"
print (isValidUpi(str6))
|
C#
using System;
using System.Text.RegularExpressions;
public class GFG
{
public static bool isValidUpi( string upi_Id)
{
string regex
= "^[a-zA-Z0-9.-]{2, 256}@[a-zA-Z][a-zA-Z]{2, 64}$" ;
Regex p = new Regex(regex);
if (upi_Id == null ) {
return false ;
}
Match m = p.Match(upi_Id);
return m.Success;
}
public static void Main()
{
string str1 = "9136812895@ybl" ;
Console.WriteLine(isValidUpi(str1));
string str2 = "rahul.12chauhan1998-1@okicici" ;
Console.WriteLine(isValidUpi(str2));
string str3 = "BNZAA2318JM" ;
Console.WriteLine(isValidUpi(str3));
string str4 = "934517865" ;
Console.WriteLine(isValidUpi(str4));
string str5 = "ViratKohli101@paytm" ;
Console.WriteLine(isValidUpi(str5));
string str6 = "Akanksha @ybl" ;
Console.WriteLine(isValidUpi(str6));
}
}
|
Javascript
function isValid_UPI_ID(upi_Id) {
let regex = new RegExp(/^[a-zA-Z0-9.-]{2, 256}@[a-zA-Z][a-zA-Z]{2, 64}$/);
if (upi_Id == null ) {
return "false" ;
}
if (regex.test(upi_Id) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "9136812895@ybl" ;
console.log(isValid_UPI_ID(str1));
let str2 = "rahul.12chauhan1998-1@okicici" ;
console.log(isValid_UPI_ID(str2));
let str3 = "BNZAA2318JM" ;
console.log(isValid_UPI_ID(str3));
let str4 = "MH 05 S 9954" ;
console.log(isValid_UPI_ID(str4));
let str5 = "ViratKohli101@paytm" ;
console.log(isValid_UPI_ID(str5));
let str6 = "Akanksha @ybl" ;
console.log(isValid_UPI_ID(str6));
|
Output
true
true
false
false
true
false
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Last Updated :
14 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...