如何使用正則表示式驗證影像副檔名?


介紹

在本文中,我們檢查如何使用正則表示式驗證影像副檔名。本文中的影像副檔名是指包含檔名和副檔名的有效影像副檔名。有效的影像副檔名遵循我們在本文中定義的一些規則。

正則表示式或正則表示式用於字串中的模式匹配或字串搜尋演算法。其功能在 <regex> 標頭檔案中定義。它與一個後跟變數的 regex 關鍵字一起使用。在本 C++ 教程中,我們使用正則表示式檢查輸入字串是否為有效的影像副檔名。我們考慮一個輸入字串,並透過匹配影像副檔名命名規則來檢查其有效性。為了驗證影像副檔名,我們使用正則表示式進行模式匹配。

語法

regex regular_expression_patternname
return regex_match(value, regular_expression_patternname);

有效影像副檔名的規則如下:

  • 影像檔案應以至少一個字元開頭。

  • 影像檔案不能為空。

  • 影像副檔名後跟一個點和檔名副檔名。例如,abc.jpg

  • 影像檔名稱中沒有空格。

  • 可接受的影像副檔名是 .jpg、.gif、.jpeg、.bmp 和 .png。

演示 1

Input = Filename = "cdf.jpeg"
Output = The input name is a valid file name.

解釋

以上字串遵循所有有效影像檔案的規則。因此,它是一個有效的影像副檔名。

演示 2

Input = Filename = "6fg.jpfg"
Output = No, it is not a valid image file.

解釋

根據影像檔案規則,以上輸入字串是無效的影像檔案。有效的影像副檔名沒有 jpfg 副檔名。

因此,它是一個無效的影像檔案。

演算法

  • 獲取一個輸入影像檔案字串。

  • 定義一個包含所有有效副檔名規則的正則表示式。

  • 定義正則表示式後,迭代輸入字串的所有字元。

  • 將輸入字串的每個字元與正則表示式模式匹配。

  • 使用條件語句確定結果。

  • 列印輸出。

正則表示式的語法

正則表示式如下所示:

^[^\s]+\.(jpg|jpeg|png|gif|bmp)$

這裡,

  • ^ = 它表示字串的開頭。

  • [^\s]= 它表示應以字元開頭的字串。^ 是否定字元類。//s 表示沒有空格

  • + = 它表示字串可以有多個字母。

  • \. = 反斜槓 (\) 用於賦予點 (.) 意義,因為這裡點僅被視為點。

  • (jpg|jpeg|png|gif|bmp) = 它是有效影像副檔名的組。豎線 (|) 充當 OR 運算子,在不同的影像副檔名之間提供選擇。

  • $ = 它表示字串的結尾。

示例 1

我們在 C++ 中實現了查詢有效輸入影像副檔名的任務。透過生成包含所有有效影像檔案規則的 match_pattern 來使用正則表示式。

#include <iostream>
#include <regex>
#include <string>
using namespace std;

//user-defined function to determine the validity of the input string using regular expression
bool isImageFileValid(const string& filename) {
   // Regular expression for a valid image file. It satisfy all the conditions of a valid image file
   regex imageRegex("^[^\s]+\.(jpg|jpeg|png|gif|bmp)$", regex_constants::icase);

   // Match the image file extension against the regular expression
   return regex_match(filename, imageRegex);
}

//code controller
int main() {
   // Predefined image file name
   string filename = "abc.ppt";

   //conditional statement to determine the result via calling the function
   if (isImageFileValid(filename)) {
      cout << "Valid image file!" << endl;
   } else {
      cout << "Invalid image file extension or filename format!" << endl;
   }

   return 0;
}

輸出

Invalid image file extension or filename format!

示例 2

我們在 C++ 中使用正則表示式實現了檢查輸入字串是否為有效影像檔案的任務。使用了不同的輸入字串。利用了一個包含所有有效影像檔案條件的正則表示式。

#include <iostream>
#include <regex>
using namespace std;

// user-defined function to verify the string
bool validImageFile(string strVar){ 

   // Regular expression containing all conditions for a valid image file extension
   const regex matchPattern("[^\s]+(.*?)\.(jpeg|jpg|png|gif|JPEG|JPG|GIF|PNG)$");
   
   // If the string is empty
   if (strVar.empty()) {
      return false;
   }
   
   // conditional statement that returns true when a string is a valid image file extension
   if(regex_match(strVar, matchPattern)) {
      return true;
   } else {
      return false;
   }
}

// Code controller
int main(){
   string strVar1 = "1de.jpeg";
   cout << validImageFile(strVar1) << endl;
   string strVar2 = "cgd.jpg";
   cout << validImageFile(strVar2) << endl;
   string strVar3 = "cfg.pnfg";
   cout << validImageFile(strVar3) << endl;
   string strVar4 = ".jpg";
   cout << validImageFile(strVar4) << endl;
   string strVar5 = "a b.jpg";
   cout << validImageFile(strVar5) << endl;
   return 0;
}

輸出

1
1
0
0
1

結論

我們已經完成了本教程,在本教程中,我們檢查輸入字串是否為有效的影像副檔名。我們使用正則表示式來檢查字串的有效性。根據有效影像副檔名的條件,我們聲明瞭一個正則表示式並將其與輸入字串匹配。使用條件語句確定結果。用一些示例演示了問題陳述,以確定任務的需求。

更新於: 2023年10月3日

2K+ 閱讀量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告