使用正則表示式驗證印度車輛牌照


在本問題中,我們將使用正則表示式驗證印度車輛牌照。

正則表示式是由不同字元建立的搜尋模式,我們可以用它來匹配給定字串中的特定模式。

問題陳述 - 我們給定一個表示印度車輛牌照的字串。我們需要使用正則表示式來驗證給定的字串。

示例

Input: num1 = "GJ 03 AY 1097"
Output: Yes

解釋 - 給定的車輛牌照有效。

Input: num2 = "TN 1A3 PZ 1287"
Output: No

解釋 - 給定的車牌無效,因為有 '1A3'。

Input: num2 = "DL 29 KJX 0001"
Output: No

解釋 - 給定的車牌無效,因為有 'KJX'。

有效的印度車輛牌照的示例為 GJ 03 AY 1097。

  • 車牌包含 2 個州編號字元。

  • 之後,它包含 2 位數字,代表區號。

  • 接下來,它包含 1 或 2 個字母字元。

  • 最後,它包含 4 位數字。

使用者可以遵循以下正則表示式來驗證印度車輛牌照。

"^[A-Z]{2}[ -]?[0-9]{2}[ -]?[A-Z]{1,2}[ -]?[0-9]{4}$"

讓我們理解一下正則表示式。

  • ^ − 它表示車牌的開頭。

  • [A - Z]{2} − 它應該包含 2 個大寫字母字元。

  • [ -]? − 之後,它可能包含空格或連字元。

  • [0-9]{2}[ -]? − 它應該包含 2 位數字,代表區號。

  • [A-Z]{1,2} − 它應該包含 1 或 2 個字母字元。

  • [0-9]{4}$ − 最後,它應該包含 4 位數字。

演算法

  • 步驟 1 − 定義名為 patt 的正則表示式模式。

  • 步驟 2 − 如果字串為空,則返回 'No'。

  • 步驟 3 − 使用 regex_match() 方法使用 'patt' 驗證車牌字串。

  • 步驟 4 − 如果 regex_match() 方法返回 true,則從函式返回 'yes'。否則,從函式返回 'No'。

示例

以下是各種程式語言中上述演算法的程式

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

// Function to check if a number plate is valid
char* checkForNumberPlate(char* numPlate) {
   // Defining the regular expression
   regex_t patt;
   int reti = regcomp(&patt, "^[A-Z]{2}[ -]?[0-9]{2}[ -]?[A-Z]{1,2}[ -]?[0-9]{4}$", REG_EXTENDED);

   if (reti) {
      fprintf(stderr, "Could not compile regex\n");
      exit(1);
   }

   // When the string is empty
   if (strlen(numPlate) == 0) {
      return "No";
   }

   // Return the answer after validating the number plate
   if (!regexec(&patt, numPlate, 0, NULL, 0)) {
      return "Yes";
   } else {
      return "No";
   }
}
int main() {
   char num1[] = "GJ 03 AY 1097";
   printf("Is %s Valid? - %s\n", num1, checkForNumberPlate(num1));
   
   char num2[] = "TN 1A3 PZ 1287";
   printf("Is %s Valid? - %s\n", num2, checkForNumberPlate(num2));

   return 0;
}

輸出

Is GJ 03 AY 1097 Valid? - Yes
Is TN 1A3 PZ 1287 Valid? - No
#include <bits/stdc++.h>
#include <regex>
using namespace std;

string checkForNumberPlate(string numPlate) {
   // Defining the regular expression
   const regex patt("^[A-Z]{2}[ -]?[0-9]{2}[ -]?[A-Z]{1,2}[ -]?[0-9]{4}$");
   // When the string is empty
   if (numPlate.empty()) {
      return "No";
   }
   // Return the answer after validating the number plate
   if (regex_match(numPlate, patt)) {
      return "Yes";
   } else {
      return "No";
   }
}
int main() {
   string num1 = "GJ 03 AY 1097";
   cout << "Is " << num1 << " Valid? - " << checkForNumberPlate(num1) << endl;   
   string num2 = "TN 1A3 PZ 1287";
   cout << "Is " << num2 << " Valid? - " << checkForNumberPlate(num2) << endl;
   return 0;
}

輸出

Is GJ 03 AY 1097 Valid? - Yes
Is TN 1A3 PZ 1287 Valid? - No
import java.util.regex.*;

public class NumberPlateValidator {
   // Function to check if a number plate is valid
   public static String checkForNumberPlate(String numPlate) {
      // Defining the regular expression
      String patt = "^[A-Z]{2}[ -]?[0-9]{2}[ -]?[A-Z]{1,2}[ -]?[0-9]{4}$";
      Pattern pattern = Pattern.compile(patt);

      // When the string is empty
      if (numPlate.isEmpty()) {
         return "No";
      }

      // Return the answer after validating the number plate
      Matcher matcher = pattern.matcher(numPlate);
      if (matcher.matches()) {
         return "Yes";
      } else {
         return "No";
      }
   }

   public static void main(String[] args) {
      String num1 = "GJ 03 AY 1097";
      System.out.println("Is " + num1 + " Valid? - " + checkForNumberPlate(num1));

      String num2 = "TN 1A3 PZ 1287";
      System.out.println("Is " + num2 + " Valid? - " + checkForNumberPlate(num2));
   }
}

輸出

Is GJ 03 AY 1097 Valid? - Yes
Is TN 1A3 PZ 1287 Valid? - No
import re

# Function to check if a number plate is valid
def checkForNumberPlate(numPlate):
   # Defining the regular expression
   patt = "^[A-Z]{2}[ -]?[0-9]{2}[ -]?[A-Z]{1,2}[ -]?[0-9]{4}$"

   # When the string is empty
   if len(numPlate) == 0:
      return "No"

   # Return the answer after validating the number plate
   if re.match(patt, numPlate):
      return "Yes"
   else:
      return "No"

if __name__ == "__main__":
   num1 = "GJ 03 AY 1097"
   print(f"Is {num1} Valid? - {checkForNumberPlate(num1)}")

   num2 = "TN 1A3 PZ 1287"
   print(f"Is {num2} Valid? - {checkForNumberPlate(num2)}")

輸出

Is GJ 03 AY 1097 Valid? - Yes
Is TN 1A3 PZ 1287 Valid? - No

時間複雜度 − O(N) 以匹配模式。

空間複雜度 − O(1),因為我們沒有使用任何額外的空間。

程式設計師可以嘗試建立另一個正則表示式來驗證印度車輛牌照。例如,它不應該允許在正則表示式之間使用空格或連字元。此外,regex_search() 方法可以使用正則表示式驗證字串。

更新於: 2023年10月27日

2K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.