1.基本字符
2.常用正则表达式
用户名 | ^[a-z0-9_-]{3,16}$ |
密码 | ^[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 \.-]*)*\/?$ |
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]+$ |
3.示例
主要有三大操作: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; }
运行结果:
参考网址: