在线文档教程
C++
规律表达 | Regular expressions

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().firstfirst
m.prefix().secondm0.first
m.prefix().matchedm.prefix().first != m.prefix().second
m.suffix().firstm0.second
m.suffix().secondlast
m.suffix().matchedm.suffix().first != m.suffix().second
m0.firstthe start of the matching sequence
m0.secondthe end of the matching sequence
m0.matchedtrue
mn.firstthe start of the sequence that matched marked sub-expression n, or last if the subexpression did not participate in the match
mn.secondthe end of the sequence that matched marked sub-expression n, or last if the subexpression did not participate in the match
mn.matchedtrue 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。

http://en.cppreference.com/w/cpp/regex/regex[医]搜索