# 正则表达式 regex
regex表示有一个正则表达式类regex\_match将一个字符序列与一个正则表达式匹配
regex\_search寻找第一个与正则表达式匹配的子序列
regex\_replace使用给定格式替换一个正则表达式
sregex\_iterator迭代适配器, 调用regex\_search来遍历一个string中所有匹配的子串smatch容器类, 保存在string中搜索的结果ssub\_matchstring中匹配的子表达式的结果
~~~
#include <iostream>
#include <string>
#include <regex>
int main(void)
{
std::string str1[] = {"foo.txt", "b.txt", "abcd.txt", "txt", "a0.txt", "AAA.txt", "wt.txt"};
std::regex txtRegex("[a-z]+\\.txt");
std::regex baseRegex("([a-z]+)\\.txt");
std::smatch baseMatch;
for (const auto &s : str1)
{
if (std::regex_match(s, baseMatch, baseRegex))
{
std::cout << "baseMatch[0]: " << baseMatch[0] << std::endl;
std::cout << "baseMatch.szie: " << baseMatch.size() << std::endl;
if (baseMatch.size() == 2)
{
std::string str = baseMatch[0].str();
std::string base = baseMatch[1].str();
std::cout << "str: " << str << std::endl;
std::cout << s << " : " << base << std::endl;
}
}
}
return 0;
}
~~~
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)
- c++基础
- 官方在线文档
- auto typename decltype类型推导
- c++常用关键字
- 取消变量未使用警告
- 变量类型
- 字面量
- 指针和引用
- 统一初始化 结构化绑定
- foreach遍历
- using使用
- alignas alignof
- 模板
- 引用和完美转发
- 逗号表达式
- 异常 try catch
- string和string_view
- c++程序退出
- 函数参数类型和个数
- lambda
- 单例模式
- c++标准库
- 文档库
- cmath 算数库
- utilities
- array 数组
- vector 动态数组
- 变量类型推断function
- 正则表达式
- 随机数
- 多线程并发
- cout输出格式化
- 常用std函数
- hash定制
- c++使用第三方库实列
- google glog日志库
- gtest单元测试
- 线程池
- c++ rpc
- Mongoose网络协议
- cpp-httplib
- json ⚡ - JSON for Modern C++
- JeayeSON c++14
- ini文件解析库
- libuv uvw
- boost
- https://www.boost.org/doc/libs/1_79_0/
- ptree ini json xml
- dll
- lockfree
- socket
- cpp serial
- c++常用库列表
- c++常用调试技巧
- 死锁
- valgrind使用
- gdb
- core 生成
- c++编程总览