std::regex_match

来自cppreference.com
< cpp‎ | regex
在标头 <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) 目标字符序列以范围 [firstlast) 表示。

如果 BidirIt 不满足老式双向迭代器 (LegacyBidirectionalIterator) 的要求,那么行为未定义。

(C++23 前)

如果 BidirIt 未实现 bidirectional_iterator,那么行为未定义。

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

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

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

如果匹配存在,那么给定 (0m.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
m[n].second
m[n].matched
  1. 匹配前缀为空。
  2. 匹配后缀为空。
  3. 匹配到了整个序列。

目录

[编辑] 参数

first, last - 以应用正则表达式的目标字符范围,以迭代器给出
str - 目标 C 风格字符串
s - 目标 std::basic_string
m - 匹配结果
e - 正则表达式
flags - 用于确定将如何进行匹配的标志

[编辑] 返回值

整个目标序列匹配 e 时返回 true,否则返回 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))
            // 首个 sub_match 是整个字符串;下个
            // sub_match 是首个有括号表达式。
            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 << "  submatch " << i << ":" << piece << '\n';
            }
        }
}

输出:

foo.txt:1
bar.txt:1
baz.dat:0
zoidberg:0
foo.txt 的基本名是 foo
bar.txt 的基本名是 bar
foo.txt
  submatch 0:foo.txt
  submatch 1:foo
  submatch 2:txt
bar.txt
  submatch 0:bar.txt
  submatch 1:bar
  submatch 2:txt
baz.dat
  submatch 0:baz.dat
  submatch 1:baz
  submatch 2:dat

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 出版时的行为 正确行为
LWG 2205 C++11 在后条件中 n 可以为零 只能为正
LWG 2273 C++11 不明确是否考虑部分匹配 只考虑完全匹配
LWG 2329 C++11 重载 (5) 接受 basic_string 右值,这可能会导致悬垂迭代器 通过被删除的重载 (7) 拒绝

[编辑] 参阅

正则表达式对象
(类模板) [编辑]
标识一个正则表达式匹配,包含所有子表达式匹配
(类模板) [编辑]
尝试匹配一个正则表达式到字符序列的任何部分
(函数模板) [编辑]