std::regex_search
来自cppreference.com
<tbody>
</tbody>
| 在标头 <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) 表示。
|
如果 |
(C++23 前) |
|
如果 |
(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
|
| ||
m[n].second
|
| ||
m[n].matched
|
| ||
参数
| first, last | - | 以应用正则表达式的目标字符范围,以迭代器给出 |
| str | - | 目标 C 风格字符串 |
| s | - | 目标 std::basic_string |
| m | - | 匹配结果 |
| e | - | 正则表达式 |
| flags | - | 用于确定将如何进行匹配的标志 |
返回值
整个目标序列匹配 e 时返回 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 << "'" << line << "' 的匹配\n";
std::cout << "前缀:'" << color_match.prefix() << "'\n";
for (std::size_t i = 0; i < color_match.size(); ++i)
std::cout << i << ":" << color_match[i] << '\n';
std::cout << "后缀:'" << 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 << "\n在位置 " << cm.prefix().length()
<< " 找到 " << cm[0] << '\n';
}
输出:
Roses are #ff0000:true
violets are #0000ff:true
all of my base are belong to you:false
'Roses are #ff0000' 的匹配
前缀:'Roses are '
0:#ff0000
1:ff
2:00
3:00
后缀:''
'violets are #0000ff' 的匹配
前缀:'violets are '
0:#0000ff
1:00
2:00
3:ff
后缀:''
Speed: 366
Speed: 378
Speed: 400
在位置 10 找到 test
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 2205 | C++11 | 在后条件中 n 可以为零
|
只能为正 |
| LWG 2329 | C++11 | 重载 (5) 接受 basic_string 右值,这可能会导致悬垂迭代器
|
通过被删除的重载 (7) 拒绝 |
参阅
(C++11) |
正则表达式对象 (类模板) |
(C++11) |
标识一个正则表达式匹配,包含所有子表达式匹配 (类模板) |
(C++11) |
尝试匹配一个正则表达式到整个字符序列 (函数模板) |