Namespaces
Variants

std:: regex_search

From cppreference.net
定义于头文件 <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) 目标字符序列由范围 [ 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 [ 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})");
    // 简单匹配
    for (const auto& line : lines)
        std::cout << line << ": " << std::boolalpha
                  << std::regex_search(line, color_regex) << '\n';
    std::cout << '\n';
    // 显示每个匹配中标记子表达式的内容
    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";
        }
    // 重复搜索(另见 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风格字符串演示
    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) 拒绝

参见

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