regex_search
描述 (Description)
它返回目标序列(主题)中的某个子序列是否与正则表达式rgx(模式)匹配。 目标序列是s或第一个和最后一个之间的字符序列,具体取决于使用的版本。
声明 (Declaration)
以下是std :: regex_search的声明。
template <class charT, class traits>
bool regex_search (const charT* s, const basic_regex<charT,traits>& rgx,
regex_constants::match_flag_type flags = regex_constants::match_default);
C++11
template <class charT, class traits>
bool regex_search (const charT* s, const basic_regex<charT,traits>& rgx,
regex_constants::match_flag_type flags = regex_constants::match_default);
C++14
template <class charT, class traits>
bool regex_search (const charT* s, const basic_regex<charT,traits>& rgx,
regex_constants::match_flag_type flags = regex_constants::match_default);
参数 (Parameters)
s - 它是一个包含目标序列的字符串。
rgx - 它是一个匹配的basic_regex对象。
flags - 用于控制rgx的匹配方式。
m - 它是match_results类型的对象。
返回值 (Return Value)
如果rgx与目标序列中的子序列匹配,则返回true。 否则是假的。
异常 (Exceptions)
No-noexcept - 这个成员函数永远不会抛出异常。
例子 (Example)
在下面的示例中为std :: regex_search。
#include <iostream>
#include <string>
#include <regex>
int main () {
std::string s ("this subject has a submarine as a subsequence");
std::smatch m;
std::regex e ("\\b(sub)([^ ]*)");
std::cout << "Target sequence: " << s << std::endl;
std::cout << "Regular expression: /\\b(sub)([^ ]*)/" << std::endl;
std::cout << "The following matches and submatches were found:" << std::endl;
while (std::regex_search (s,m,e)) {
for (auto x:m) std::cout << x << " ";
std::cout << std::endl;
s = m.suffix().str();
}
return 0;
}
输出应该是这样的 -
Target sequence: this subject has a submarine as a subsequence
Regular expression: /\b(sub)([^ ]*)/
The following matches and submatches were found:
subject sub ject
submarine sub marine
subsequence sub sequence