正则表达式在处理字符串方面具有很强大的功能。正则表达式是包含一系列字符的表达式,这些字符定义了可用于字符串搜索算法、查找或查找/替换算法等的特定搜索模式。正则表达式也用于输入验证。
各个符号的含义 ^ $^:限定开头的字符 $:限定结尾的字符d D s S w W
d:匹配数字字符 D:匹配非数字字符 s:匹配空格 S:匹配非空格 w:匹配一般字符(字母,数字,下划线) W:匹配除了字母/数字、下划线外的字符.
.:匹配任意单个字符* + ? {…}
*:*前的单个字符可以出现任意次(单个字符包括空格)
+:+前的单个字符至少出现一次(单个字符不包括空格)
?:?前的单个字符最多出现一次
{...}:
() {} []
()分组:(123),这样可以将匹配到的123取出来
{}长度:{4,9},这个表示前一个字符串的长度为4到9
[]范围:[a-z],这个表示匹配所有的小写字母
C++中的正则表达式
从C++11开始,C++ 通过标准库提供正则表达式支持。在使用时需要包含头文件regex。
regex_matchregex_match可以理解为全匹配,在写正则时,需要与待匹配字符串格式保持一致。
测试 ^ $ 1. “^A.*” :以A开头的字符串 代码#include结果 2. “.*A$” 以A结尾的字符串 代码#include #include int main(int argc, char** argv) { while (1) { std::string inputData; std::getline(std::cin, inputData); std::regex regexStr("^A.*"); std::smatch matchResult; if (std::regex_match(inputData, matchResult, regexStr)) { std::cout << "Matched" << std::endl; for (auto ele : matchResult) { std::cout << ele << std::endl; } } else { std::cout << "Not matched" << std::endl; } } return 0; }
#include结果 d D s S w W 1. "^\d." :以数字字符开头的字符串 代码#include #include int main(int argc, char** argv) { while (1) { std::string inputData; std::getline(std::cin, inputData); std::regex regexStr(".*A$"); std::smatch matchResult; if (std::regex_match(inputData, matchResult, regexStr)) { std::cout << "Matched" << std::endl; for (auto ele : matchResult) { std::cout << ele << std::endl; } std::cout << "n"; } else { std::cout << "Not matched" << "n" << std::endl; } } return 0; }
#include结果 2. “^\D.*”:以非数字字符开头的字符串 代码#include #include int main(int argc, char** argv) { while (1) { std::string inputData; std::getline(std::cin, inputData); std::regex regexStr("^\d.*"); std::smatch matchResult; if (std::regex_match(inputData, matchResult, regexStr)) { std::cout << "Matched" << std::endl; for (auto ele : matchResult) { std::cout << ele << std::endl; } std::cout << "n"; } else { std::cout << "Not matched" << "n" << std::endl; } } return 0; }
#include结果 3. “.\s.*” 第二个字符为空格的字符串 代码#include #include int main(int argc, char** argv) { while (1) { std::string inputData; std::getline(std::cin, inputData); std::regex regexStr("^\D.*"); std::smatch matchResult; if (std::regex_match(inputData, matchResult, regexStr)) { std::cout << "Matched" << std::endl; for (auto ele : matchResult) { std::cout << ele << std::endl; } std::cout << "n"; } else { std::cout << "Not matched" << "n" << std::endl; } } return 0; }
#include结果 4. “.\S.*” 第二个字符不为空格的字符串 代码#include #include int main(int argc, char** argv) { while (1) { std::string inputData; std::getline(std::cin, inputData); std::regex regexStr(".\s.*"); std::smatch matchResult; if (std::regex_match(inputData, matchResult, regexStr)) { std::cout << "Matched" << std::endl; for (auto ele : matchResult) { std::cout << ele << std::endl; } std::cout << "n"; } else { std::cout << "Not matched" << "n" << std::endl; } } return 0; }
#include结果#include #include int main(int argc, char** argv) { while (1) { std::string inputData; std::getline(std::cin, inputData); std::regex regexStr(".\S.*"); std::smatch matchResult; if (std::regex_match(inputData, matchResult, regexStr)) { std::cout << "Matched" << std::endl; for (auto ele : matchResult) { std::cout << ele << std::endl; } std::cout << "n"; } else { std::cout << "Not matched" << "n" << std::endl; } } return 0; }



