正则表达式在 c++11 中引入. 在 c++ 正则表达式库中, 主要包括一些类, 算法, 迭代器,
标识等. 在编写程序时, 调用的入口是正则表达式库中的算法和迭代器.
主要实体
类
basic_regex
正则表达式类, 封装了正则表达式字符串. 根据模板参数不同, 又可以分为
-
类 定义 regex basic_regex <char>
wregex basic_regex <wchar_t>
- 表 1
-
match_results
表示正则表达式匹配, 其内可能包含多个子匹配. 根据模板参数不同,
又可以分为-
类 定义 csub_match sub_match <const char*>
wcsub_match sub_match <const wchar_t*>
ssub_match sub_match <std::string::const_iterator>
wssub_match sub_match <std::wstring::const_iterator>
- 表 2
自 c++17 开始, 还引入了下面类型的 match_results, 它们定义在 std::pmr 名字空间
-
类 定义 std::pmr::cmatch std::pmr::match_results <const char*>
std::pmr::wcmatch std::pmr::match_results <const wchar_t*>
std::pmr::smatch std::pmr::match_results <std::string::const_iterator>
std::pmr::wsmatch std::pmr::match_results <std::wstring::const_iterator>
- 表 3
pmr 是 polymorphic memory resource 的缩写, 是指多态内存资源.
-
sub_match
表示正则表达式的子匹配. 根据模板参数不同, 又可以分为
扫描二维码关注公众号,回复: 1579281 查看本文章类 定义 csub_match sub_match <const char*>
wcsub_match sub_match <const wchar_t*>
ssub_match sub_match <std::string::const_iterator>
wssub_match sub_match <std::wstring::const_iterator>
: 表 4
算法
regex_match
完全匹配. 要求给定的正则表达式匹配整个源字符串.
正则表达式中也可以包含子匹配.regex_search
部分匹配. 给定的正则表达式与给定的源字符串部分匹配即可. 从字面理解,
就是从源字符串中搜索正则表达式代表的字字符串.regex_replace
替换. 将源字符串中符合正则表达式的子字符串替换为指定的其它字符串.
迭代器
regex_iterator
用于枚举源字符串中与给定正则表达式匹配的子字符串.
regex_token_iterator
用于枚举源字符串由给定正则表达式匹配的子字符串分割的子字符串.
与给定正则表达式匹配的子字符串称为 token. 源串由 token 分割为多个子字符串.
常量
syntax_option_type
控制对正则表达式的解析, 比如使用何种正则表达语法 (如 ECMAScript,
sed 等), 正则表达式是否区分大小写等.match_flag_type
控制正则匹配的行为.
其它
- regex_error, 异常类.
- error_type, 表示正则表达式分析过程中的错误类型.
- regex_traits, 特性类.
主要实体之间的关系
下面描述的主要实体之间的关系时, 忽略 std::pmr 内的相关实体与 [其它]
中描述的实体.
下面给出一个总图, 描述了各个实体之间的依赖关系.
图 1 总图
下面分别画出主要实体对其它实体之间的依赖关系.
图 2 regex_match
图 3 regex_search
图 4 regex_replace
图 5 regex_iterator
图 6 regex_token_iterator
后记
在学习了 c++ 正则表达式库, 经过很长时间后会忘记如何使用,
此时通过画的图可以很容易想起如何使用 c++ 正则表达式.