Namespaces
Variants

std:: regex_match

From cppreference.net
定义于头文件 <regex>
template < class BidirIt, class Alloc, class CharT, class Traits >

bool regex_match ( 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 ) ;
(1) (C++11 起)
template < class BidirIt, class CharT, class Traits >

bool regex_match ( BidirIt first, BidirIt last,
const std:: basic_regex < CharT, Traits > & e,
std:: regex_constants :: match_flag_type flags =

std:: regex_constants :: match_default ) ;
(2) (C++11 起)
template < class CharT, class Alloc, class Traits >

bool regex_match ( 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 ) ;
(3) (C++11 起)
template < class CharT, class Traits >

bool regex_match ( const CharT * str, const std:: basic_regex < CharT, Traits > & e,
std:: regex_constants :: match_flag_type flags =

std:: regex_constants :: match_default ) ;
(4) (C++11 起)
template < class STraits, class SAlloc, class Alloc,

class CharT, class Traits >
bool regex_match
( 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 ) ;
(5) (C++11 起)
template < class STraits, class SAlloc, class CharT, class Traits >

bool regex_match ( 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 ) ;
(6) (C++11 起)
template < class STraits, class SAlloc, class Alloc,

class CharT, class Traits >
bool regex_match
( 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 ;
(7) (C++11 起)

判断正则表达式 e 是否与目标字符序列完全匹配。详细匹配结果将存储于 m 中(若存在该参数)。

1,2) 目标字符序列由范围 [ first , last ) 表示。

BidirIt 不满足 LegacyBidirectionalIterator 的要求,则行为未定义。

(C++23 前)

BidirIt 不建模 bidirectional_iterator ,则行为未定义。

(C++23 起)
3,4) 目标字符序列由范围 [ str , str + std:: char_traits < CharT > :: length ( str ) ) 表示。
5,6) 目标字符序列由字符串 s 表示。
7) 目标字符序列无法通过 std::string 右值表示。

如果匹配不存在,涉及 m 的以下表达式(如果存在)应产生指定值:

表达式
m. ready ( ) true
m. size ( ) 0
m. empty ( ) true

如果匹配存在,给定任意整数在 ( 0 , m. size ( ) ) 范围内作为 n ,则涉及 m 的下列表达式应对下方列出的每个重载产生指定值:

表达式
重载 (1) 重载 (3) 重载 (5)
m. ready ( ) true
m. size ( ) 1 + e. mark_count ( )
m. empty ( ) false
m. prefix ( ) . first first str s. begin ( )
m. prefix ( ) . second
m. prefix ( ) . matched false [1]
m. suffix ( ) . first last std:: char_traits < CharT > ::
length ( str ) + str
s. end ( )
m. suffix ( ) . second
m. suffix ( ) . matched false [2]
m [0] . first first str s. begin ( )
m [0] . second last std:: char_traits < CharT > ::
length ( str ) + str
s. end ( )
m [0] . matched true [3]
m [n] . first
  • 如果 标记子表达式 n 未参与匹配,则为 last
  • 否则为匹配子表达式 n 的序列起始位置
m [n] . second
  • 如果 标记子表达式 n 未参与匹配,则为 last
  • 否则为匹配子表达式 n 的序列结束位置
m [n] . matched
  1. 匹配前缀为空。
  2. 匹配后缀为空。
  3. 完整序列被匹配。

目录

参数

first, last - 目标字符范围
str - 目标以空字符结尾的C风格字符串
s - 目标 std::basic_string
m - 匹配结果
e - 正则表达式
flags - 用于确定匹配执行方式的标志位

返回值

如果整个目标序列与 e 匹配则返回 true ,否则返回 false

注释

由于 regex_match 仅考虑完全匹配,同一正则表达式在 regex_match std::regex_search 之间可能产生不同的匹配结果:

std::regex re("Get|GetValue");
std::cmatch m;
std::regex_search("GetValue", m, re);  // 返回 true,且 m[0] 包含 "Get"
std::regex_match ("GetValue", m, re);  // 返回 true,且 m[0] 包含 "GetValue"
std::regex_search("GetValues", m, re); // 返回 true,且 m[0] 包含 "Get"
std::regex_match ("GetValues", m, re); // 返回 false

示例

#include <cstddef>
#include <iostream>
#include <regex>
#include <string>
int main()
{
    // 简单正则表达式匹配
    const std::string fnames[] = {"foo.txt", "bar.txt", "baz.dat", "zoidberg"};
    const std::regex txt_regex("[a-z]+\\.txt");
    for (const auto& fname : fnames)
        std::cout << fname << ": " << std::regex_match(fname, txt_regex) << '\n';
    // 提取子匹配项
    const std::regex base_regex("([a-z]+)\\.txt");
    std::smatch base_match;
    for (const auto& fname : fnames)
        if (std::regex_match(fname, base_match, base_regex))
            // 第一个子匹配是整个字符串;下一个
            // 子匹配是第一个括号内的表达式。
            if (base_match.size() == 2)
            {
                std::ssub_match base_sub_match = base_match[1];
                std::string base = base_sub_match.str();
                std::cout << fname << " 的基名为 " << base << '\n';
            }
    // 提取多个子匹配项
    const std::regex pieces_regex("([a-z]+)\\.([a-z]+)");
    std::smatch pieces_match;
    for (const auto& fname : fnames)
        if (std::regex_match(fname, pieces_match, pieces_regex))
        {
            std::cout << fname << '\n';
            for (std::size_t i = 0; i < pieces_match.size(); ++i)
            {
                std::ssub_match sub_match = pieces_match[i];
                std::string piece = sub_match.str();
                std::cout << "  子匹配 " << i << ": " << piece << '\n';
            }
        }
}

输出:

foo.txt: 1
bar.txt: 1
baz.dat: 0
zoidberg: 0
foo.txt 的基名为 foo
bar.txt 的基名为 bar
foo.txt
  子匹配 0: foo.txt
  子匹配 1: foo
  子匹配 2: txt
bar.txt
  子匹配 0: bar.txt
  子匹配 1: bar
  子匹配 2: txt
baz.dat
  子匹配 0: baz.dat
  子匹配 1: baz
  子匹配 2: dat

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 应用于 发布时行为 正确行为
LWG 2205 C++11 后置条件中 n 可能为零 必须为正数
LWG 2273 C++11 未明确是否考虑部分匹配 仅考虑完全匹配
LWG 2329 C++11 重载 (5) 接受 basic_string 右值,
可能导致悬垂迭代器
通过已删除的重载 (7) 拒绝

参见

正则表达式对象
(类模板)
标识一个正则表达式匹配,包括所有子表达式匹配
(类模板)
尝试将正则表达式与字符序列的任何部分进行匹配
(函数模板)