<regex>

定义一个类模板来分析正则表达式 (C++),以及定义多个类模板和函数以在文本中搜索正则表达式对象的匹配项。

语法

#include <regex>

备注

若要创建正则表达式对象,请使用类模板 basic_regex 或其专用化之一, regex 以及 wregex类型的 regex_constants::syntax_option_type语法标志。

若要在文本中搜索正则表达式对象的匹配项,请将模板函数 regex_matchregex_search 与类型为 regex_constants::match_flag_type 的匹配项标志一起使用。 这些函数通过使用类模板match_results及其专用化、cmatch、、smatchwcmatch以及wsmatch类模板sub_match及其专用化、csub_matchwcsub_matchssub_match以及wssub_match它们返回结果。

若要替换匹配正则表达式对象的文本,请将模板函数 regex_replace 与类型为 regex_constants::match_flag_type 的匹配项标志一起使用。

若要循环访问正则表达式对象的多个匹配项,请将类模板 regex_iteratorregex_token_iterator或其专用化(cregex_iteratorsregex_iteratorwcregex_iteratorwsregex_iteratorcregex_token_iteratorsregex_token_iteratorwcregex_token_iteratorwsregex_token_iterator)之一与类型为 regex_constants::match_flag_type 的匹配项标志一起使用。

若要修改正则表达式语法的详细信息,请编写一个实现正则表达式特征的类。

说明
basic_regex 包装正则表达式。
match_results 包含一系列子匹配项。
regex_constants 包含各种类型的常量。
regex_error 报告错误的正则表达式。
regex_iterator 循环访问匹配结果。
regex_traits 描述用于匹配的元素的特征。
regex_traits<char> 描述用于匹配的 char 的特征。
regex_traits<wchar_t> 描述用于匹配的 wchar_t 的特征。
regex_token_iterator 循环访问子匹配项。
sub_match 介绍子匹配项。

类型定义

名称 说明
cmatch 的类型定义。charmatch_results
cregex_iterator 的类型定义。charregex_iterator
cregex_token_iterator 的类型定义。charregex_token_iterator
csub_match 的类型定义。charsub_match
regex 的类型定义。charbasic_regex
smatch 的类型定义。stringmatch_results
sregex_iterator 的类型定义。stringregex_iterator
sregex_token_iterator 的类型定义。stringregex_token_iterator
ssub_match 的类型定义。stringsub_match
wcmatch 的类型定义。wchar_tmatch_results
wcregex_iterator 的类型定义。wchar_tregex_iterator
wcregex_token_iterator 的类型定义。wchar_tregex_token_iterator
wcsub_match 的类型定义。wchar_tsub_match
wregex 的类型定义。wchar_tbasic_regex
wsmatch 的类型定义。wstringmatch_results
wsregex_iterator 的类型定义。wstringregex_iterator
wsregex_token_iterator 的类型定义。wstringregex_token_iterator
wssub_match 的类型定义。wstringsub_match

函数

函数 说明
regex_match 与正则表达式完全匹配。
regex_replace 替换匹配正则表达式。
regex_search 搜索正则表达式匹配项。
swap 交换 basic_regexmatch_results 对象。

运算符

运算符 说明
operator== 比较各种对象,相等。
operator!= 比较各种对象,不相等。
operator< 比较各种对象,小于。
operator<= 比较各种对象,小于或等于。
operator> 比较各种对象,大于。
operator>= 比较各种对象,大于或等于。
operator<< sub_match 插入流中。

另请参阅

正则表达式 (C++)
regex_constants
regex_error
<regex> 函数
regex_iterator
<regex> 运算符
regex_token_iterator
regex_traits
<regex> typedef