命名空间
变体
操作

std::regex_search

来自 cppreference.cn
< cpp‎ | regex
在头文件 <regex> 中定义
template< class BidirIt, class Alloc, class CharT, class Traits >

bool regex_search( 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_search( 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_search( 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_search( 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_search
    ( 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_search( 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_search
    ( 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[0].first
m.prefix().matched m.prefix().first != m.prefix().second
m.suffix().first m[0].second
m.suffix().second last std::char_traits<CharT>::
    length(str) + str
s.end()
m.suffix().matched m.suffix().first != m.suffix().second
m[0].first 匹配 e 的序列的开始
m[0].second 匹配 e 的序列的结束
m[0].matched true
m[n].first
  • 如果 标记子表达式 n 未参与匹配,则为 last
  • 否则为匹配子表达式 n 的序列的开始
m[n].second
  • 如果 标记子表达式 n 未参与匹配,则为 last
  • 否则为匹配子表达式 n 的序列的结束
m[n].matched

目录

[编辑] 参数

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

[编辑] 返回值

如果存在匹配,则返回 true,否则返回 false

[编辑] 注意

为了检查目标序列中的所有匹配,可以循环调用 std::regex_search,每次从上次调用的 m[0].second 开始。 std::regex_iterator 提供了这种迭代的简单接口。

[编辑] 示例

#include <cstddef>
#include <iostream>
#include <regex>
#include <string>
 
int main()
{
    std::string lines[] = {"Roses are #ff0000",
                           "violets are #0000ff",
                           "all of my base are belong to you"};
 
    std::regex color_regex("#([a-f0-9]{2})"
                            "([a-f0-9]{2})"
                            "([a-f0-9]{2})");
 
    // simple match
    for (const auto& line : lines)
        std::cout << line << ": " << std::boolalpha
                  << std::regex_search(line, color_regex) << '\n';
    std::cout << '\n';
 
    // show contents of marked subexpressions within each match
    std::smatch color_match;
    for (const auto& line : lines)
        if (std::regex_search(line, color_match, color_regex))
        {
            std::cout << "matches for '" << line << "'\n";
            std::cout << "Prefix: '" << color_match.prefix() << "'\n";
            for (std::size_t i = 0; i < color_match.size(); ++i) 
                std::cout << i << ": " << color_match[i] << '\n';
            std::cout << "Suffix: '" << color_match.suffix() << "\'\n\n";
        }
 
    // repeated search (see also std::regex_iterator)
    std::string log(R"(
        Speed:	366
        Mass:	35
        Speed:	378
        Mass:	32
        Speed:	400
	Mass:	30)");
    std::regex r(R"(Speed:\t\d*)");
    for (std::smatch sm; regex_search(log, sm, r);)
    {
        std::cout << sm.str() << '\n';
        log = sm.suffix();
    }
 
    // C-style string demo
    std::cmatch cm;
    if (std::regex_search("this is a test", cm, std::regex("test"))) 
        std::cout << "\nFound " << cm[0] << " at position "
                  << cm.prefix().length() << '\n';
}

输出

Roses are #ff0000: true
violets are #0000ff: true
all of my base are belong to you: false
 
matches for 'Roses are #ff0000'
Prefix: 'Roses are '
0: #ff0000
1: ff
2: 00
3: 00
Suffix: ''
 
matches for 'violets are #0000ff'
Prefix: 'violets are '
0: #0000ff
1: 00
2: 00
3: ff
Suffix: ''
 
Speed:	366
Speed:	378
Speed:	400
 
Found test at position 10

[编辑] 缺陷报告

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

缺陷报告 应用于 发布时的行为 正确的行为
LWG 2205 C++11 后置条件中 n 可以为零 只能为正
LWG 2329 C++11 重载 (5) 接受了 basic_string 右值,
这可能导致悬空迭代器
通过删除重载 (7) 拒绝

[编辑] 另请参阅

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