<regex>

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

语法

#include <regex>

备注

若要创建正则表达式对象,请将类模板 basic_regex或其专用化(regexwregex)之一与类型为 regex_constants::syntax_option_type 的语法标志一起使用。

若要在文本中搜索正则表达式对象的匹配项,请将模板函数 regex_matchregex_search 与类型为 regex_constants::match_flag_type 的匹配项标志一起使用。 这些函数通过将类模板 match_results及其专用化(cmatchwcmatchsmatchwsmatch)与类模板 sub_match及其专用化(csub_matchwcsub_matchssub_matchwssub_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