std::regex_search
STD::regex[医]搜索
Defined in header | | |
---|---|---|
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) | (since 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 | (2) | (since 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 | (3) | (since 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 | (4) | (since 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 | (5) | (since 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) | (since 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) | (since C++14) |
确定正则表达式之间是否匹配。e
以及目标字符序列中的一些子序列。
1%29分析泛型范围[first,last)
中返回匹配结果m
...
2%29分析以空结尾的字符串。str
中返回匹配结果m
...
3%29分析字符串s
中返回匹配结果m
...
4-6%29等于%281-3%29,只是省略了匹配结果.
7%29重载3被禁止接受临时字符串,否则此函数填充匹配。[医]结果:m与字符串迭代器一起,这些迭代器立即失效。
regex_search
将成功匹配给定序列的任何子序列,而std::regex_match
只会回来true
如果正则表达式与全
顺序。
参数
first, last | - | a range identifying the target character sequence |
---|---|---|
str | - | a pointer to a null-terminated target character sequence |
s | - | a string identifying target character sequence |
e | - | the std::regex that should be applied to the target character sequence |
m | - | the match results |
flags | - | std::regex_constants::match_flag_type governing search behavior |
类型要求
-Bidirit必须符合双向迭代器的要求。
-分配器必须符合分配器的要求。
返回值
回报true
如果有匹配,false
否则。在任何一种情况下,对象m
更新如下:
如果不存在匹配:
m就绪%28%29==真
*。
空%28%29==真
M.尺寸%28%29=0
如果存在匹配:
m.ready() | true |
---|---|
m.empty() | false |
m.size() | number of marked subexpressions plus 1, that is, 1+e.mark_count() |
m.prefix().first | first |
m.prefix().second | m0.first |
m.prefix().matched | m.prefix().first != m.prefix().second |
m.suffix().first | m0.second |
m.suffix().second | last |
m.suffix().matched | m.suffix().first != m.suffix().second |
m0.first | the start of the matching sequence |
m0.second | the end of the matching sequence |
m0.matched | true |
mn.first | the start of the sequence that matched marked sub-expression n, or last if the subexpression did not participate in the match |
mn.second | the end of the sequence that matched marked sub-expression n, or last if the subexpression did not participate in the match |
mn.matched | true if sub-expression n participated in the match, false otherwise |
注记
为了检查目标序列中的所有匹配,std::regex_search
可以在循环中调用,每次从m[0].second
之前的电话。std::regex_iterator
为这个迭代提供了一个简单的接口。
例
二次
#include <iostream>
#include <string>
#include <regex>
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 (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*)"
std::smatch sm;
while(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(
}
二次
产出:
二次
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
二次
另见
basic_regex (C++11) | regular expression object (class template) |
---|---|
match_results (C++11) | identifies one regular expression match, including all sub-expression matches (class template) |
regex_match (C++11) | attempts to match a regular expression to an entire character sequence (function template) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。