C++参悟:正则表达式库regex(更新中)

news/2025/2/5 4:51:57/

正则表达式库regex(更新中)

  • 一、概述
  • 二、快速上手Demo
    • 1. 查找字符串
    • 2. 匹配字符串
    • 3. 替换字符串
  • 三、类关系梳理
    • 1. 主类
      • 1. basic_regex
    • 2. 算法
    • 3. 迭代器
    • 4. 异常
    • 5. 特征
    • 6. 常量
      • 1. syntax_option_type
      • 2. match_flag_type
      • 3. error_type

一、概述

C++标准库为我们提供了处理字符串的正则表达式库。正则表达式是一种用于在字符串中匹配模式的微型语言。

正则表达式在查询、替换字符串的时候有很多快速的使用场景,是一个经常使用的工具。正则表达式需要使用到正则表达式的语法,这个语法是独立于编程语言外的一个工具。这个可以 在线查看和测试

菜鸟学习教程 :https://www.runoob.com/regexp/regexp-syntax.html
在这里插入图片描述

在线测试工具 :https://stackoverflow.org.cn/regex/
在这里插入图片描述

这个用到的头文件便是:

#include <regex>

二、快速上手Demo

1. 查找字符串

1. 采用迭代器查找

// 待匹配字符串
std::string s = "Some people, when confronted with a problem";// 正则表达式匹配对象-匹配单词
std::regex word_regex("(\\w+)");// 获取迭代器
auto words_begin = std::sregex_iterator(s.begin(), s.end(), word_regex);
auto words_end = std::sregex_iterator();// 总元素个数
int count = std::distance(words_begin, words_end);// 遍历元素
for (std::sregex_iterator i = words_begin; i != words_end; ++i) {//  查询到的匹配对象std::smatch match = *i;// 输出这个匹配对象的内容std::string match_str = match.str();std::cout << "  " << match_str << '\n';
}/* 输出结果
Some 
people
when 
confronted
with
a
problem
*/

2. 使用算法查找

使用 std::regex_search() 查找

//常用
template< class BidirIt,class Alloc, class CharT, class Traits >
bool regex_search( BidirIt first, BidirIt last,std::match_results<BidirIt,Alloc>& m,const std::basic_regex<CharT,Traits>& e,std::regex_constants::match_flag_type flags = std::regex_constants::match_default ); 
//常用
template< class CharT, class Alloc, class Traits >
bool regex_search( const CharT* str,std::match_results<const CharT*,Alloc>& m,const std::basic_regex<CharT,Traits>& e,std::regex_constants::match_flag_type flags = std::regex_constants::match_default ); 
//常用
template< class STraits, class SAlloc,class Alloc, class CharT, class Traits >
bool regex_search( const std::basic_string<CharT,STraits,SAlloc>& s,std::match_results<typename std::basic_string<CharT,STraits,SAlloc>::const_iterator, Alloc>& m,const std::basic_regex<CharT, Traits>& e,std::regex_constants::match_flag_type flags = std::regex_constants::match_default ); template< class BidirItclass CharT, class Traits >
bool regex_search( BidirIt first, BidirIt last,const std::basic_regex<CharT,Traits>& e,std::regex_constants::match_flag_type flags = std::regex_constants::match_default );template< class CharT, class Traits >
bool regex_search( const CharT* str,const std::basic_regex<CharT,Traits>& e,std::regex_constants::match_flag_type flags = std::regex_constants::match_default ); template< class STraits, class SAlloc,class CharT, class Traits >
bool regex_search( const std::basic_string<CharT,STraits,SAlloc>& s,const std::basic_regex<CharT,Traits>& e,std::regex_constants::match_flag_type flags =std::regex_constants::match_default );template< class STraits, class SAlloc,class Alloc, class CharT, class Traits >
bool regex_search( const std::basic_string<CharT,STraits,SAlloc>&&,std::match_results<typename std::basic_string<CharT,STraits,SAlloc>::const_iterator, Alloc>&,const std::basic_regex<CharT, Traits>&,std::regex_constants::match_flag_type flags = std::regex_constants::match_default ) = delete; 

这个函数的部分参数说明

  • 待匹配字符串(三种输入)
    1. first, last - 标识目标字符序列的范围
    2. str - 指向空终止字符序列的指针
    3. s - 标识目标字符序列的指针
  • e - 应当应用到目标字符序列的 std::regex :其实就是正则匹配对象
  • m - 匹配结果 :用的是 match_results 对象描述
  • flags - 掌管搜索行为的 std::regex_constants::match_flag_type
// 待匹配字符串
std::string lines[] = {"Roses are #ff0000","violets are #0000ff","all of my base are belong to you"};
// 正则表达式对象
std::regex color_regex("#([a-f0-9]{2})""([a-f0-9]{2})""([a-f0-9]{2})");
// 匹配结果
std::vector<std::smatch> matchs;// 简单匹配
for (const auto &line : lines) {std::smatch m;std::cout << line << ": " << std::boolalpha<< std::regex_search(line, m, color_regex) << "\n";matchs.push_back(m);
}// 输出结果
for(auto m: matchs){if(m.ready() && !m.empty())std::cout << "Useful Color: " << m.str() << "\n";
}
/* 输出结果
Roses are #ff0000: true
violets are #0000ff: true
all of my base are belong to you: false
Useful Color: #ff0000
Useful Color: #0000ff
*/

2. 匹配字符串

3. 替换字符串

三、类关系梳理

1. 主类

这些类封装正则表达式和在字符的目标序列中匹配正则表达式的结果。

1. basic_regex

basic_regex :正则表达式对象

在源代码里面看到那个 std::regex 对象就是用这个 basic_regex 定义的

/** @brief Standard regular expressions. */
typedef basic_regex<char>    regex;

sub_match :标识子表达式所匹配的字符序列

match_results:标识一个正则表达式匹配,包含所有子表达式匹配

2. 算法

这些算法将封装于 regex 的正则表达式应用到字符的目标序列。

regex_match:尝试匹配一个正则表达式到整个字符序列

regex_search:尝试匹配一个正则表达式到字符序列的任何部分

regex_replace:以格式化的替换文本来替换正则表达式匹配的出现位置

3. 迭代器

regex_iterator:用于遍历在序列中找到的匹配正则表达式的整个集合。

regex_iterator :迭代一个字符序列中的所有正则表达式匹配

regex_token_iterator:迭代给定字符串中的所有正则表达式匹配中的指定子表达式,或迭代未匹配的子字符串

4. 异常

此类定义作为异常抛出以报告来自正则表达式库错误的类型。

regex_error :报告正则表达式库生成的错误

使用这个也是非常简单的

#include <regex>
#include <iostream>int main()
{try {std::regex re("[a-b][a");}catch (const std::regex_error& e) {std::cout << "regex_error caught: " << e.what() << '\n';// 这个错误码定义在 6.常量.error_type 中if (e.code() == std::regex_constants::error_brack) {std::cout << "The code was error_brack\n";}}
}// 输出
regex_error caught: The expression contained mismatched [ and ].
The code was error_brack

5. 特征

regex_traits 类用于封装 regex 的本地化方面。

regex_traits:提供正则表达式库所需的关于字符类型的元信息

6. 常量

定义于命名空间 std::regex_constants

1. syntax_option_type

syntax_option_type: 控制正则表达式行为的通用选项

2. match_flag_type

match_flag_type:特定于匹配的选项

3. error_type

error_type:描述不同类型的匹配错误,可以用 try catch 捕获

constexpr error_type error_collate = /*unspecified*/;
constexpr error_type error_ctype = /*unspecified*/;
constexpr error_type error_escape = /*unspecified*/;
constexpr error_type error_backref = /*unspecified*/;
constexpr error_type error_brack = /*unspecified*/;
constexpr error_type error_paren = /*unspecified*/;
constexpr error_type error_brace = /*unspecified*/;
constexpr error_type error_badbrace = /*unspecified*/;
constexpr error_type error_range = /*unspecified*/;
constexpr error_type error_space = /*unspecified*/;
constexpr error_type error_badrepeat = /*unspecified*/;
constexpr error_type error_complexity = /*unspecified*/;
constexpr error_type error_stack = /*unspecified*/;

名词解释如下:

常量解释
error_collate表达式含有非法对照字符名
error_ctype表达式含有非法字符类名
error_escape表达式含有非法转义字符或尾随转义
error_backref表达式含有非法回溯引用
error_brack表达式含有不匹配的方括号对( ‘[’ 与 ‘]’ )
error_paren表达式含有不匹配的括号对( ‘(’ 与 ‘)’ )
error_brace表达式含有不匹配的花括号对( ‘{’ 与 ‘}’ )
error_badbrace表达式在 {} 表达式中含有非法范围
error_range表达式含有非法字符范围(例如 [b-a] )
error_space没有将表达式转换成有限状态机的足够内存
error_badrepeat*?+{ 之一不后继一个合法正则表达式
error_complexity尝试的匹配的复杂度超过了预定义的等级
error_stack没有进行匹配的足够内存

http://www.ppmy.cn/news/1326956.html

相关文章

【深度学习】BasicSR训练过程记录

文章目录 两种灵活的使用场景项目结构概览简化的使用方式 项目结构解读1. 代码的入口和训练的准备工作2. data和model的创建2.1 dataloader创建2.2 model的创建 3. 训练过程 动态实例化的历史演进1. If-else判断2. 动态实例化3. REGISTER注册机制 REGISTER注册机制的实现1. DAT…

Jenkins实现CICD(1)_Windows10 安装Jenkins

文章目录 一、打开Jenkins官网&#xff0c;下载安装包二、安装Jenkins三、JAVA环境_JDK17下载安装&#xff08;Windows版&#xff09;四、将jdk-17添加到系统环境变量五、jenkins关联jdk-17六、安装常用插件(例如&#xff1a;git、gitlab、钉钉) 一、打开Jenkins官网&#xff0…

C++从零开始的打怪升级之路(day14)

这是关于一个普通双非本科大一学生的C的学习记录贴 在此前&#xff0c;我学了一点点C语言还有简单的数据结构&#xff0c;如果有小伙伴想和我一起学习的&#xff0c;可以私信我交流分享学习资料 那么开启正题 今天分享的内容是string类 这里给上官方的文档链接&#xff0c;…

在线录屏-通过Web API接口轻松实现录屏

在线录屏是指在互联网上进行屏幕录制的过程。它允许用户通过网络连接&#xff0c;将自己的屏幕活动记录下来&#xff0c;并可以在需要时进行播放、共享或存档。在线录屏常用于教育、培训、演示、游戏等场景&#xff0c;可以帮助用户展示操作步骤、解决问题、分享经验等。通常&a…

计算机组成原理 I/O方式

I/O 方式 I/O方式分类: 程序查询方式。由 CPU通过程序不断查询 /O 设备是否已做好准备&#xff0c;从而控制0 设备与主机交换信息程序中断方式。只在 I/0 设备准备就绪并向 CPU发出中断请求时才予以响应。DMA方式。主存和 I/O 设备之间有一条直接数据通路&#xff0c;当主存和…

Wheeltec小车的开发实录(0)

配置静态ip&#xff08;可以联网&#xff09; 首先在你正常链接网络的时候打开“Connection Information”(我的是wifi&#xff0c;而且是手机热点&#xff0c;所以我手机就相当于一台路由器) 查看路由ip 观察到Default Route 是192.168.***.225这就是我手机的地址&#xff0…

Maven 依赖传递和冲突、继承和聚合

一、依赖传递和冲突 1.1 Maven 依赖传递特性 1.1.1 概念 假如有三个 Maven 项目 A、B 和 C&#xff0c;其中项目 A 依赖 B&#xff0c;项目 B 依赖 C。那么我们可以说 A 依赖 C。也就是说&#xff0c;依赖的关系为&#xff1a;A—>B—>C&#xff0c; 那么我们执行项目 …

无法打开浏览器开发者工具的可能解决方法

网页地址: https://jx.xyflv.cc/?url视频地址url 我在抖音里面抓了一个视频地址, 获取到响应的json数据, 找到里面的视频地址信息 这个网站很好用: https://www.jsont.run/ 可以使用js语法对json对象操作, 找到所有视频的url地址 打开网页: https://jx.xyflv.cc/?urlhttps:…