使用正則表示式驗證UPI ID


在這個問題中,我們將使用正則表示式來驗證UPI ID。UPI是統一支付介面,分配給每個客戶,其他人可以使用它向您轉賬。

  • UPI ID包含字母數字字元。

  • UPI ID必須包含“@”字元。

  • UPI ID不能包含空格。

  • UPI ID可以包含點(.)或連字元(-)。

問題陳述 - 我們以字串格式給定一個UPI ID。我們需要使用正則表示式來驗證UPI ID。

示例

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

解釋

UPI ID包含“@”和字母數字字元。所以它是有效的。

Input: upi = "shubhamokaxis";
Output: No

解釋

UPI ID不包含“@”字元。所以它不是有效的UPI ID。

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

解釋

UPI ID包含“:”,這是一個無效字元。

正則表示式

正則表示式是一個用於識別字符串中模式的字串。以下是驗證UPI的正則表示式。

"^[0-9A-Za-z.-]{2,256}@[A-Za-z]{2,64}$"
  • ^ − 正則表示式的開頭。

  • [0-9A-Za-z.-]{2,256} − 開頭應包含2到256個字母數字、點或連字元字元。

  • @ − 它必須包含@字元。

  • [A-Za-z]{2,64} − 在“@”字元之後,它應包含2到64個字母字元。

方法一

在這種方法中,我們將使用“regex_match”方法來驗證正則表示式。

演算法

  • 步驟1 - 匯入“regex”庫以使用與正則表示式相關的函式。

  • 步驟2 - 使用“regex”資料型別建立一個名為“upiPatt”的正則表示式。

  • 步驟3 - 使用empty()方法檢查字串是否為空。如果是,則返回false。

  • 步驟4 - 使用“regex_match()”方法,並將UPI字串作為第一個引數,正則表示式作為第二個引數。如果該方法返回true,則從函式返回true。否則,返回false。

示例

以下是各種程式語言中此操作的實現:

#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;
}

輸出

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;
}

輸出

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!");
      }
   }
}

輸出

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!")

輸出

Yes, it is a valid UPI ID!

時間複雜度 – O(N) 用於匹配模式。

空間複雜度 – O(1),因為我們不使用任何動態空間。

方法二

在這種方法中,我們將使用regex_search()方法使用正則表示式驗證UPI ID。

演算法

  • 步驟1 - 使用“regex”資料型別建立正則表示式。

  • 步驟2 - 如果字串為空,則返回false。

  • 步驟3 - 將UPI和正則表示式作為regex_search()方法的引數來驗證UPI ID。

  • 步驟4 - 根據regex_search()方法的返回值返回true或false。

示例

以下是上述方法的程式

#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;
}	

輸出

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;
}

輸出

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!");
      }
   }
}

輸出

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!")

輸出

Yes, it is a valid UPI ID!

時間複雜度 – O(N) 用於驗證UPI ID。

空間複雜度 – O(1)

regex_search()和regex_match()方法用於使用正則表示式驗證字串,但它們在輸入字串與模式的匹配行為方面存在一些細微差別。程式設計師可以使用任何方法來使用正則表示式驗證UPI ID。

更新於:2023年10月27日

2K+ 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.