std::regex_search
定义于头文件 <regex> |
||
template< class BidirIt, class Alloc, class CharT, class Traits > bool regex_search( BidirIt first, BidirIt last, |
(1) | (since C++11) |
template< class BidirIt, class CharT, class Traits > bool regex_search( BidirIt first, BidirIt last, |
(2) | (since C++11) |
template< class CharT, class Alloc, class Traits > bool regex_search( const CharT* str, |
(3) | (since C++11) |
template< class CharT, class Traits > bool regex_search( const CharT* str, const std::basic_regex<CharT, Traits>& e, |
(4) | (since C++11) |
template< class STraits, class SAlloc, class Alloc, class CharT, class Traits > |
(5) | (since C++11) |
template< class STraits, class SAlloc, class CharT, class Traits > bool regex_search( const std::basic_string<CharT, STraits, SAlloc>& s, |
(6) | (since C++11) |
template< class STraits, class SAlloc, class Alloc, class CharT, class Traits > |
(7) | (since C++11) |
确定目标字符序列中是否存在正则表达式 e 的匹配项。详细的匹配结果存储在 m 中(如果存在)。
[
first,
last)
表示。
如果 |
(C++23 前) |
如果 |
(C++23 起) |
[
str,
str + std::char_traits<CharT>::length(str))
表示。如果不存在匹配项,则涉及 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 | - | 用于确定如何执行匹配的标志 |
[编辑] 返回值
如果存在匹配项,则返回 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++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 2205 | C++11 | 后置条件中 n 可能为零 | 只能为正数 |
LWG 2329 | C++11 | 重载 (5) 接受 basic_string 右值, 这可能导致悬空迭代器 |
通过删除的重载 (7) 拒绝 |
[编辑] 参见
(C++11) |
正则表达式对象 (类模板) |
(C++11) |
标识一个正则表达式匹配项,包括所有子表达式匹配项 (类模板) |
(C++11) |
尝试将正则表达式匹配到整个字符序列 (函数模板) |