#include #include #include #include namespace std
int main()
// *等價於匹配長度
// +等價於匹配長度
// 轉義字元等價\w \w \d \d \. \\,但是注意c++中要使用\\w, \\w, \\d, \\d, \\\\
rx = "^[a-za-z]\\.doc$";// 等價於*,表示零個以上,以.doc結尾
rx = "[^0-9]";// 不包含0-9的零個以上個字元(中放開頭的^表示不等於,若不放開頭為普通字元)
rx = "^abc[a-za-z]*abc$";// ^abc表示以abc開始,abc$表示以abc結束
rx = "^1827918[0-9]$";// 以1827918開頭,後接四位數字結束
rx = "^abc[0-9a-za-z_]*cba$";// 以abc開頭,中間新增[0-9a-za-z_]範圍內的數,後接cba結束
// 好像表示開頭和結尾的^和$都沒什麼用
rx = "[-+]*[0-9]*\\.?[0-9]*";// 數
rx = ".*";// .表示任何單位字元(全部範圍)
rx = "[0-9a-za-z_]+\\.txt";// 非空檔名的txt
} catch (std::exception & e)
std::string s;
while (std::cin >> s)
// 2.對wchar_t
std::wstring ws;
std::wregex wrx;// wregex是basic_regex的typedef
try\\.?[0-9]";
} catch (std::exception & e)
while (std::wcin >> ws)
// 3.特別說明對\\,對字串中的\\,正則中用\\\\匹配字串中的\\。
wrx2 = l"[a-za-z]:\\\\[^^%&',;=?$\"<>:\\\\]*\\.jpg";// 不允許在碟符下的子目錄中。判斷是否有\\用\\\\匹配
std::cout << std::boolalpha << std::regex_match(path, wrx2) << std::endl;
return 0;
}
C 11正規表示式
優勢 使得字串的處理更加簡單 一些相關的操作 驗證 檢查字串是否是想要的合法性 決策 判斷乙個輸入標書哪種字串 解析 從輸入的字串中查詢自己想要的資訊 轉換 搜尋字串,並將字串替換為新的格式化的字串 遍歷 搜尋字串所有出現的地方 符號化 根據一組分隔符將乙個字串分解為多個子字串 一些重要術語 模式 ...
c 11 正規表示式
include include 正規表示式標頭檔案 using namespace std regex search 檢索 regex replace 將檢索到的物件進行替換替換 match 是否匹配 void main cout 正規表示式實現字串的替換 void main 匹配時間 void m...
C 11 正規表示式
0.常用正規表示式 中文字元 u4e00 u9fa5 雙位元組字元 包括漢字在內 x00 xff 空白符 n s r 國內 號碼 d d d 18位身份證號 d d d d d 0 9 x 年 月 日 格式日期 0 9 1 9 0 9 1 9 0 9 0 9 1 9 0 9 1 9 0 9 0 13...