命名空间
变体
操作

std::regex_match

来自 cppreference.cn
< 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) (since C++11) (始于 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) (since C++11) (始于 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) (since C++11) (始于 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) (since C++11) (始于 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) (since C++11) (始于 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) (since C++11) (始于 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) (since C++11) (始于 C++11)

确定正则表达式 e 是否匹配整个目标字符序列。详细的匹配结果存储在 m 中(如果存在)。

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

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

(until C++23) (直到 C++23)

如果 BidirIt 不符合 bidirectional_iterator 模型,则行为未定义。

(since C++23) (始于 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

[编辑] 注解

由于 regex_match 仅考虑完整匹配,因此相同的正则表达式在 regex_matchstd::regex_search 之间可能给出不同的匹配结果

std::regex re("Get|GetValue");
std::cmatch m;
std::regex_search("GetValue", m, re);  // returns true, and m[0] contains "Get"
std::regex_match ("GetValue", m, re);  // returns true, and m[0] contains "GetValue"
std::regex_search("GetValues", m, re); // returns true, and m[0] contains "Get"
std::regex_match ("GetValues", m, re); // returns false

[编辑] 示例

#include <cstddef>
#include <iostream>
#include <regex>
#include <string>
 
int main()
{
    // Simple regular expression matching
    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';
 
    // Extraction of a sub-match
    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))
            // The first sub_match is the whole string; the next
            // sub_match is the first parenthesized expression.
            if (base_match.size() == 2)
            {
                std::ssub_match base_sub_match = base_match[1];
                std::string base = base_sub_match.str();
                std::cout << fname << " has a base of " << base << '\n';
            }
 
    // Extraction of several sub-matches
    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 has a base of foo
bar.txt has a base of 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++ 标准。

DR 应用于 已发布行为 正确行为
LWG 2205 C++11 n 在后置条件中可能为零 只能为正数
LWG 2273 C++11 不清楚是否考虑部分匹配 仅考虑完整匹配
LWG 2329 C++11 重载 (5) 接受 basic_string 右值,
这可能导致悬空迭代器
通过删除的重载 (7) 拒绝

[编辑] 参见

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