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

正则表达式库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.hqwc.cn/news/419496.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mapboxGL中的航线动画

概述 借用上篇文章中二阶贝塞尔曲线的生成&#xff0c;本文实现mapboxGL中的航线动画。 效果 实现 1. 初始化地图 const from [101.797439042302, 36.5937248286007]; const to [106.9733, 35.217]; const points new ArcLine(from, to); const line new Geometry(poin…

【开源】基于JAVA语言的贫困地区人口信息管理系统

目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 人口信息管理模块2.2 精准扶贫管理模块2.3 特殊群体管理模块2.4 案件信息管理模块2.5 物资补助模块 三、系统设计3.1 用例设计3.2 数据库设计3.2.1 人口表3.2.2 扶贫表3.2.3 特殊群体表3.2.4 案件表3.2.5 物资补助表 四…

java验证ftp地址是否可用

一.前言 在实际开发中我们的业务是我们将订单发到客户的指定的地方, 我们需要验证用户的ftp地址是否真实且有效, 我们根据java程序来进行验证, 步骤和思路应该是. 步骤描述1导入所需要的 java类库(jar包依赖)2创建ftp客户端对象3设置ftp连接服务端的连接参数4建立与ftp的服务…

IntelliJ IDEA 常用快捷键一览表(通用型,提高编写速度,类结构、查找和查看源码,替换与关闭,调整格式)

文章目录 IntelliJ IDEA 常用快捷键一览表1-IDEA的日常快捷键第1组&#xff1a;通用型第2组&#xff1a;提高编写速度&#xff08;上&#xff09;第3组&#xff1a;提高编写速度&#xff08;下&#xff09;第4组&#xff1a;类结构、查找和查看源码第5组&#xff1a;查找、替换…

【C++】文件操作

文件操作 一、文本文件&#xff08;一&#xff09;写文件读文件 二、二进制文件&#xff08;一&#xff09;写文件&#xff08;二&#xff09;读文件 程序运行时产生的数据都属于临时数据&#xff0c;程序一旦运行结束都会被释放&#xff0c;通过文件可以将数据持久化&#xff…

JRTP实时音视频传输(2)-使用TCP通信的案例

环境搭建等参考&#xff1a;JRTP实时音视频传输(1)-必做的环境搭建与demo测试 1.创建自己的demo 先将example1拷贝为myclienttcp.cpp和myservertcp.cpp cp example1.cpp myclienttcp.cpp cp example1.cpp myservertcp.cpp 改写jrtplib/JRTPLIB/examples/CMakeLists.txt&…

用Axure RP 9制作弹出框

制作流程 1.准备文本框 下拉列表 按钮 动态面板 如图 2.先把下拉列表放好 再放动态面板覆盖 3.点动态面板 进入界面 如图 4.给按钮添加交互 3个按钮一样的 如图 5.提交按钮添加交互 如图

python222网站实战(SpringBoot+SpringSecurity+MybatisPlus+thymeleaf+layui)-热门标签推荐显示实现

锋哥原创的SpringbootLayui python222网站实战&#xff1a; python222网站实战课程视频教程&#xff08;SpringBootPython爬虫实战&#xff09; ( 火爆连载更新中... )_哔哩哔哩_bilibilipython222网站实战课程视频教程&#xff08;SpringBootPython爬虫实战&#xff09; ( 火…

Eyes Wide Shut? Exploring the Visual Shortcomings of Multimodal LLMs

大开眼界&#xff1f;探索多模态模型种视觉编码器的缺陷。 论文中指出&#xff0c;上面这些VQA问题&#xff0c;人类可以瞬间给出正确的答案&#xff0c;但是多模态给出的结果却是错误的。是哪个环节出了问题呢&#xff1f;视觉编码器的问题&#xff1f;大语言模型出现了幻觉&…

【计算机网络】TCP握手与挥手:三步奏和四步曲

这里写目录标题 前言三次握手四次挥手三次握手和四次挥手的作用TCP三次握手的作用建立连接防止已失效的连接请求建立连接防止重复连接 TCP四次挥手的作用&#xff1a;安全关闭连接避免数据丢失避免半开连接 总结&#xff1a; 总结 前言 TCP&#xff08;传输控制协议&#xff09…

大模型理论基础3

模型架构 模型概括 先把语言模型看成黑盒&#xff0c;以便于了解整体功能后拆分成&#xff1a;分词、模型架构 分词 首先要知道&#xff1a;语言模型 p 是建立在词元&#xff08;token&#xff09;序列的上的一个概率分布输出&#xff0c;其中每个词元来自某个词汇表V&#…

NLP论文阅读记录 - 2022 | WOS 04.基于 XAI 的强化学习方法,用于社交物联网内容的文本摘要

文章目录 前言0、论文摘要一、Introduction1.1目标问题1.2相关的尝试1.3本文贡献 二.相关工作三.本文方法3.1 总结为两阶段学习3.1.1 基础系统 3.2 重构文本摘要 四 实验效果4.1数据集4.2 对比模型4.3实施细节4.4评估指标4.5 实验结果4.6 细粒度分析 五 总结思考 前言 XAI-Base…