Namespaces
Variants

std:: match_results

From cppreference.net
Regular expressions library
Classes
(C++11)
match_results
(C++11)
Algorithms
Iterators
Exceptions
Traits
Constants
(C++11)
Regex Grammar
定义于头文件 <regex>
template <

class BidirIt,
class Alloc = std:: allocator < std:: sub_match < BidirIt >>

> class match_results ;
(1) (C++11 起)
namespace pmr {

template < class BidirIt >
using match_results = std :: match_results < BidirIt,
std:: pmr :: polymorphic_allocator <
std:: sub_match < BidirIt >>> ;

}
(2) (C++17 起)

类模板 std::match_results 用于保存表示正则表达式匹配结果的字符序列集合。

这是一个特化的分配器感知容器。它只能通过默认构造、从 std::regex_iterator 获取,或通过 std::regex_search std::regex_match 修改。由于 std::match_results 持有 std::sub_match 对象(每个对象都是指向被匹配原始字符序列的迭代器对),若原始字符序列已被销毁或其迭代器因其他原因失效,此时检查 std::match_results 将导致未定义行为。

std::match_result 中包含的第一个 std::sub_match (索引 0)始终表示正则表达式在目标序列中匹配的完整结果,而后续的 std::sub_match 则表示按顺序对应于正则表达式中界定子表达式的左括号的子表达式匹配结果。

std::match_results 满足 AllocatorAwareContainer SequenceContainer 的要求,但仅支持复制赋值、移动赋值以及为常量容器定义的操作,且比较函数的语义与容器所需语义有所不同。

目录

类型要求

-
BidirIt 必须满足 LegacyBidirectionalIterator 的要求。
-
Alloc 必须满足 Allocator 的要求。

特化

针对常见字符序列类型提供了若干特化版本:

定义于头文件 <regex>
类型 定义
std::cmatch std :: match_results < const char * >
std::wcmatch std :: match_results < const wchar_t * >
std::smatch std :: match_results < std :: string :: const_iterator >
std::wsmatch std :: match_results < std :: wstring :: const_iterator >
std::pmr::cmatch (C++17) std :: pmr :: match_results < const char * >
std::pmr::wcmatch (C++17) std :: pmr :: match_results < const wchar_t * >
std::pmr::smatch (C++17) std :: pmr :: match_results < std :: string :: const_iterator >
std::pmr::wsmatch (C++17) std :: pmr :: match_results < std :: wstring :: const_iterator >

成员类型

成员类型 定义
allocator_type Allocator
value_type std:: sub_match < BidirIt >
const_reference const value_type &
reference value_type&
const_iterator 由实现定义 (取决于底层容器)
iterator const_iterator
difference_type std:: iterator_traits < BidirIt > :: difference_type
size_type std:: allocator_traits < Alloc > :: size_type
char_type std:: iterator_traits < BidirIt > :: value_type
string_type std:: basic_string < char_type >

成员函数

构造对象
(公开成员函数)
析构对象
(公开成员函数)
赋值内容
(公开成员函数)
返回关联的分配器
(公开成员函数)
状态
检查结果是否可用
(公开成员函数)
大小
检查匹配是否成功
(公开成员函数)
返回完全建立结果状态中的匹配数量
(公开成员函数)
返回可能的最大子匹配数量
(公开成员函数)
元素访问
返回特定子匹配的长度
(公开成员函数)
返回特定子匹配首字符的位置
(公开成员函数)
返回特定子匹配的字符序列
(公开成员函数)
返回指定的子匹配
(公开成员函数)
返回目标序列起始与完整匹配起始之间的子序列
(公开成员函数)
返回完整匹配结束与目标序列结束之间的子序列
(公开成员函数)
迭代器
返回子匹配列表起始的迭代器
(公开成员函数)
返回子匹配列表末尾的迭代器
(公开成员函数)
格式化
格式化匹配结果以输出
(公开成员函数)
修改器
交换内容
(公开成员函数)

非成员函数

(C++20 中移除)
按字典序比较两个匹配结果中的值
(函数模板)
特化 std::swap 算法
(函数模板)