Validating UPI IDs using Regular Expressions


In this problem, we will validate the UPI id using the regular expression. The UPI is the unified payment interface that is given to each customer, and other people can use it to transfer money to you.

  • The UPI id contains alphanumeric characters.

  • The UPI id should always contain a ‘@’ character.

  • The UPI id should not have white spaces.

  • The UPI id may have a dot(.) or hyphen(-).

Problem statement − We have given an upi id in the string format. We need to validate the UPI id using the regular expression.

Sample Examples

Input: upi = "shubham@okaxis"
Output: ‘Yes’

Explanation

The UPI id contains ‘@’ and alphanumeric characters. So, it is valid.

Input: upi = "shubhamokaxis";
Output: No

Explanation

The UPI id doesn’t contain a ‘@’ character. So, it is not a valid UPI id.

Input: upi = "shubham@:okaxis";
Output: No

Explanation

The UPI id contains ‘:’, which is not a valid character.

Regular Expression

The regular expression is a string to identify the pattern in the string. Here is the regular expression to validate the UPI.

"^[0-9A-Za-z.-]{2,256}@[A-Za-z]{2,64}$"
  • ^ − Start of the regular expression.

  • [0-9A-Za-z.-]{2,256} − In the start, it should contain 2 to 256 alphanumeric, dot, or hyphen characters.

  • @ − It should always contain @ character.

  • [A-Za-z]{2,64} − After the ‘@’ character, it should contain 2 to 64 alphabetical characters.

Approach 1

In this approach, we will use the ‘regex_match’ method to validate the regular expression.

Algorithm

  • Step 1 − Import the ‘regex’ library to use a method related to a regular expression.

  • Step 2 − Use the ‘regex’ data type to create a regular expression named ‘upiPatt’.

  • Step 3 − Use the empty() method to check whether a string is empty. If yes, return false.

  • Step 4 − Use the ‘regex_match()’ method, and pass the UPI string as the first parameter and regular expression as a second parameter. If the method returns true, return true from the function. Otherwise, return false.

Example

Following are the implementations of this operation in various programming languages −

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <regex.h>

bool validateUPI(char* upi) {
   regex_t upiPatt;
   int ret;

   // Compile the regular expression
   ret = regcomp(&upiPatt, "^[0-9A-Za-z.-]{2,256}@[A-Za-z]{2,64}$", REG_EXTENDED);

   if (ret) {
      fprintf(stderr, "Could not compile regex\n");
      return false;
   }

   // Handling the empty string
   if (strlen(upi) == 0) {
      return false;
   }

   // Matching the UPI and regular expression
   ret = regexec(&upiPatt, upi, 0, NULL, 0);
   regfree(&upiPatt);

   if (!ret) {
      return true;
   } else {
      return false;
   }
}
int main() {
   char upi[] = "shubham@okaxis";
   if (validateUPI(upi)) {
      printf("Yes, it is a valid UPI ID!\n");
   } else {
      printf("No, it is not a valid UPI ID!\n");
   }
   return 0;
}

Output

Yes, it is a valid UPI ID!
#include <bits/stdc++.h>
#include <regex>
using namespace std;

bool validateUPI(string upi) {
   // Defining the regular expression
   const regex upiPatt("^[0-9A-Za-z.-]{2,256}@[A-Za-z]{2,64}$");
   
   // Handling the empty string
   if (upi.empty()) {
      return false;
   }
   
   // Matching the UPI and regular expression
   if (regex_match(upi, upiPatt)) {
      return true;
   } else {
      return false;
   }
}
int main() { 
   string upi = "shubham@okaxis";
   if (validateUPI(upi)) {
      cout << "Yes, it is a valid UPI ID!";
   } else {
      cout << "No, it is not a valid UPI ID!";
   }
   return 0;
}

Output

Yes, it is a valid UPI ID!
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
   static boolean validateUPI(String upi) {
      Pattern upiPatt = Pattern.compile("^[0-9A-Za-z.-]{2,256}@[A-Za-z]{2,64}$");

      // Handling the empty string
      if (upi.isEmpty()) {
         return false;
      }

      // Matching the UPI and regular expression
      Matcher matcher = upiPatt.matcher(upi);
      if (matcher.matches()) {
         return true;
      } else {
         return false;
      }
   }

   public static void main(String[] args) {
      String upi = "shubham@okaxis";
      if (validateUPI(upi)) {
         System.out.println("Yes, it is a valid UPI ID!");
      } else {
         System.out.println("No, it is not a valid UPI ID!");
      }
   }
}

Output

Yes, it is a valid UPI ID!
import re

def validate_upi(upi):
   upi_patt = r"^[0-9A-Za-z.-]{2,256}@[A-Za-z]{2,64}$"

   # Handling the empty string
   if not upi:
      return False

   # Matching the UPI and regular expression
   if re.match(upi_patt, upi):
      return True
   else:
      return False

if __name__ == "__main__":
   upi = "shubham@okaxis"
   if validate_upi(upi):
      print("Yes, it is a valid UPI ID!")
   else:
      print("No, it is not a valid UPI ID!")

Output

Yes, it is a valid UPI ID!

Time complexity – O(N) to match the pattern.

Space complexity – O(1), as we don’t use any dynamic space.

Approach 2

In this approach, we will use the regex_search() method to validate the UPI id using the regular expression.

Algorithm

  • Step 1 − Create the regular expression using the ‘regex’ data type.

  • Step 2 − If the string is empty, return false.

  • Step 3 − Pass the UPI and regex as a parameter of the regex_search() method to validate the UPI id.

  • Step 4 − Return true or false based on returned value from the regex_search() method.

Example

Following are the programs for the above approach

#include <stdio.h>
#include <stdbool.h>
#include <regex.h>

bool checkForUPI(char* upi) {
   regex_t upiRegex;
    
   // Defining the regular expression
   if (regcomp(&upiRegex, "^[0-9A-Za-z.-]{2,256}@[A-Za-z]{2,64}$", REG_EXTENDED) != 0) {
      return false;
   }
    
   // For empty strings
   if (upi[0] == '\0') {
      return false;
   }

   // Using the regexec() method
   if (regexec(&upiRegex, upi, 0, NULL, 0) == 0) {
      regfree(&upiRegex);
      return true;
   } else {
      regfree(&upiRegex);
      return false;
   }
}

int main() {
   char upi[] = "abcd@oksbi";
   
   if (checkForUPI(upi)) {
      printf("Yes, it is a valid UPI ID!\n");
   } else {
      printf("No, it is not a valid UPI ID!\n");
   }
   
   return 0;
}	

Output

Yes, it is a valid UPI ID!
#include <bits/stdc++.h>
#include <regex>
using namespace std;

bool checkforUPI(string upi) {
   // Defining the regular expression
   const regex upiPatt("^[0-9A-Za-z.-]{2,256}@[A-Za-z]{2,64}$");
   
   // For empty strings
   if (upi == "") {
      return false;
   }
   
   // Using the regex_search() method
   if (regex_search(upi, upiPatt)) {
      return true;
   } else {
      return false;
   }
}
int main() { 
   string upi = "abcd@oksbi";
   if (checkforUPI(upi)) {
      cout << "Yes, it is a valid UPI ID!";
   } else {
      cout << "No, it is not a valid UPI ID!";
   }
   return 0;
}

Output

Yes, it is a valid UPI ID!
import java.util.regex.*;

public class Main {
   public static boolean checkForUPI(String upi) {
      // Defining the regular expression
      String upiPattern = "^[0-9A-Za-z.-]{2,256}@[A-Za-z]{2,64}$";
       
      // For empty strings
      if (upi.isEmpty()) {
         return false;
      }
       
      // Using the Pattern and Matcher classes
      Pattern pattern = Pattern.compile(upiPattern);
      Matcher matcher = pattern.matcher(upi);
       
      if (matcher.find()) {
         return true;
      } else {
         return false;
      }
   }

   public static void main(String[] args) {
      String upi = "abcd@oksbi";
      
      if (checkForUPI(upi)) {
         System.out.println("Yes, it is a valid UPI ID!");
      } else {
         System.out.println("No, it is not a valid UPI ID!");
      }
   }
}

Output

Yes, it is a valid UPI ID!
import re

def check_for_upi(upi):
   # Defining the regular expression
   upi_pattern = r"^[0-9A-Za-z.-]{2,256}@[A-Za-z]{2,64}$"
   
   # For empty strings
   if not upi:
      return False
   
   # Using the regex.search() method
   if re.search(upi_pattern, upi):
      return True
   else:
      return False

if __name__ == "__main__":
   upi = "abcd@oksbi"
    
   if check_for_upi(upi):
      print("Yes, it is a valid UPI ID!")
   else:
      print("No, it is not a valid UPI ID!")

Output

Yes, it is a valid UPI ID!

Time complexity – O(N) to validate UPI id.

Space complexity – O(1)

The regex_search() and regex_match()methods are used to validate the string using the regular expression, but both have some minimal differences in their matching behavior of the input string with the pattern. Programmers may use any method to validate the UPI id using the regular expression.

Updated on: 27-Oct-2023

727 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements