C++
标准库中的 <regex> 头文件提供了正则表达式的功能,允许开发者使用一种非常灵活的方式来搜索、替换或分割字符串。正则表达式是一种强大的文本处理工具,广泛应用于数据验证、文本分析和模式匹配等领域。
正则表达式是一种使用单个字符串来描述、匹配一系列符合某个句法规则的字符串的模式。在
C++ 中,正则表达式通过 <regex> 库实现。
基本语法
正则表达式的基本组成
字符类:如 [abc] 表示匹配 a、b 或 c 中的任意一个字符。
量词:如 *(零次或多次)、+(一次或多次)、?(零次或一次)。
边界匹配:如 ^(行的开始)、$(行的结束)。
分组:使用圆括号 () 来创建一个分组。
C++ <regex> 库的主要类和函数
std::regex:表示一个正则表达式对象。
std::regex_match:检查整个字符串是否与正则表达式匹配。
std::regex_search:在字符串中搜索与正则表达式匹配的部分。
std::regex_replace:替换字符串中与正则表达式匹配的部分。
std::sregex_iterator:迭代器,用于遍历所有匹配项。
实例
1. 检查字符串是否匹配正则表达式
#include <iostream> #include <string> #include <regex> int main(){ std::string text ="Hello, World!"; std::regex pattern("^[a-zA-Z]+, [a-zA-Z]+!$"); if(std::regex_match(text, pattern)){ std::cout<<"The string matches the pattern."<< std::endl; }else{ std::cout<<"The string does not match the pattern."<< std::endl; } return0; }
输出结果:
Thestring matches the pattern.
2. 在字符串中搜索匹配项
#include <iostream> #include <string> #include <regex> int main(){ std::string text ="123-456-7890 and 987-654-3210"; std::regex pattern("\d{3}-\d{3}-\d{4}");
std::smatch matches; while(std::regex_search(text, matches, pattern)){ std::cout<<"Found: "<< matches[0]<< std::endl; text = matches.suffix().str(); } return0; }