用戶名html |
^[a-z0-9_-]{3,16}$jquery |
密碼ios |
^[a-z0-9_-]{6,18}$正則表達式 |
十六進制值 |
^#?([a-f0-9]{6}|[a-f0-9]{3})$測試 |
電子郵箱 |
^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$ |
URL |
^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$.net |
IP 地址 |
((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?) |
HTML 標籤 |
^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$ |
刪除代碼\\註釋 |
(?<!http:|\S)//.*$ |
Unicode編碼中的漢字範圍 |
^[\u2E80-\u9FFF]+$ |
主要有三大操做:regex_match,regex_search,regex_replace
// Regex.cpp : 定義控制檯應用程序的入口點。 // #include "stdafx.h" #include <regex> #include <string> #include <vector> #include <iostream> #include <stdlib.h> using namespace std; int test_regex_match() { std::string pattern{ "\\d{3}-\\d{8}|\\d{4}-\\d{7}" }; // fixed telephone std::regex re(pattern); std::vector<std::string> str{ "010-12345678", "0319-9876543", "021-123456789" }; /* std::regex_match: 判斷一個正則表達式(參數re)是否匹配整個字符序列str,它主要用於驗證文本 注意,這個正則表達式必須匹配被分析串的所有,不然返回false;若是整個序列被成功匹配,返回true */ for (auto tmp : str) { bool ret = std::regex_match(tmp, re); if (ret) fprintf(stderr, "%s, can match\n", tmp.c_str()); else fprintf(stderr, "%s, can not match\n", tmp.c_str()); } return 0; } int test_regex_search() { std::string pattern{ "^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+" }; // url std::regex re(pattern); std::vector<std::string> str{ "http://www.baidu.com", "http://www.cplusplus.com/reference/regex/regex_search/", "abcd://124.456", "abcd http://www.cplusplus.com/reference/regex/regex_search/ 123" }; /* std::regex_search: 相似於regex_match,但它不要求整個字符序列徹底匹配 能夠用regex_search來查找輸入中的一個子序列,該子序列匹配正則表達式re */ for (auto tmp : str) { bool ret = std::regex_search(tmp, re); if (ret) fprintf(stderr, "%s, can search\n", tmp.c_str()); else fprintf(stderr, "%s, can not search\n", tmp.c_str()); } return 0; } int test_regex_replace() { // reference: http://www.cplusplus.com/reference/regex/regex_replace/ std::string s("there is a subsequence1 in the string\n"); std::regex e("\\b(sub)([a-z]*)(\\d)"); // matches words beginning by "sub" //(1) std::cout << std::regex_replace(s, e, "-test-"); // using string/c-string (3) version: std::cout << std::regex_replace(s, e, "sub-$2-$3"); // using range/c-string (6) version: std::string result; std::regex_replace(std::back_inserter(result), s.begin(), s.end(), e, "$2"); std::cout << result; // with flags: std::cout << std::regex_replace(s, e, "$1 and $2", std::regex_constants::format_no_copy); std::cout << std::endl; std::cout << std::regex_replace(s, e, "$1 and $2"); std::cout << std::endl; return 0; } int _tmain(int argc, _TCHAR* argv[]) { regex reg("[0-9]{14}"); //regex reg("[0-9]{4}[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{2}"); //regex reg("\\d{4}\\d{2}\\d{2}\\d{2}\\d{2}\\d{2}"); //regex reg("^\\d+-"); smatch sm; string strTest = "1911-20180201111505-1-1"; if (regex_search(strTest, sm, reg)) { for (int i = 0; i < sm.size(); ++i) { string str = sm[i]; cout << str << endl ; } strTest = sm.suffix().str(); //cout << strTest << " "; } cout << endl; regex reg2("Windows(?=95|98|NT|2000)"); strTest = "Windows2000"; if (regex_search(strTest, sm, reg2)) { for (int i = 0; i < sm.size(); ++i) { string str = sm[i]; cout << str << endl; } strTest = sm.suffix().str(); } cout << endl; //字符串中文替換測試 string str = "呃abc"; str = str.replace(0, 2, "鬼"); cout << str << endl; cout << endl; test_regex_match(); std::cout << std::endl; test_regex_search(); std::cout << std::endl; test_regex_replace(); std::cout << std::endl; system("pause"); return 0; }
運行結果:
參考網址: