函式模板
<regex>

std::regex_search

C字串 (1)
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);
字串 (2)
template <class ST, class SA, char charT, class traits>  bool regex_search (const basic_string<charT,ST,SA>& s,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
範圍 (3)
template <class BidirectionalIterator, class charT, class traits>  bool regex_search (BidirectionalIterator first, BidirectionalIterator last,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
帶 match_results (4,5,6)
template <class charT, class Alloc, class traits>  bool regex_search (const charT* s, match_results<const charT*, Alloc>& m,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);template <class ST, class SA, class Alloc, class charT, class traits>  bool regex_search (const basic_string<charT,ST,SA>& s,          match_results<typename basic_string<charT,ST,SA>::const_iterator,Alloc>& m,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);template <class BidirectionalIterator, class Alloc, class charT, class traits>  bool regex_search (BidirectionalIterator first, BidirectionalIterator last,          match_results<BidirectionalIterator, Alloc>& m,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
C字串 (1)
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);
字串 (2)
template <class ST, class SA, char charT, class traits>  bool regex_search (const basic_string<charT,ST,SA>& s,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
範圍 (3)
template <class BidirectionalIterator, class charT, class traits>  bool regex_search (BidirectionalIterator first, BidirectionalIterator last,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
帶 match_results (4,5,6)
template <class charT, class Alloc, class traits>  bool regex_search (const charT* s, match_results<const charT*, Alloc>& m,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);template <class ST, class SA, class Alloc, class charT, class traits>  bool regex_search (const basic_string<charT,ST,SA>& s,          match_results<typename basic_string<charT,ST,SA>::const_iterator,Alloc>& m,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);template <class BidirectionalIterator, class Alloc, class charT, class traits>  bool regex_search (BidirectionalIterator first, BidirectionalIterator last,          match_results<BidirectionalIterator, Alloc>& m,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
移動字串(已刪除)(7)
template <class ST, class SA, class Alloc, class charT, class traits>  bool regex_search (const basic_string<charT,ST,SA>&&,          match_results<typename basic_string<charT,ST,SA>::const_iterator,Alloc>&,          const basic_regex<charT,traits>&,          regex_constants::match_flag_type=regex_constants::match_default) = delete;
搜尋序列
返回“目標序列”(主體)中的某個子序列是否匹配正則表示式“rgx”(模式)。“目標序列”是“s”或介於“first”和“last”之間的字元序列,具體取決於所使用的版本。

版本4、5和6與版本1、2和3分別相同,只是它們接受一個match_results型別的物件作為引數,該物件會被匹配結果資訊填充。

可選引數“flags”允許指定匹配表示式的方式。

此函式返回true如果表示式在目標序列的任何位置找到,即使序列在匹配之前或之後包含更多未匹配的字元。對於返回true僅當整個序列匹配時,請參閱regex_match。

引數

s
一個帶有目標序列(主體)的字串,用於搜尋正則表示式的匹配。
rgx
一個basic_regex物件(模式)進行匹配。
標誌
用於控制“rgx”如何匹配的標誌。
可以將這些常量中的一個或多個組合(使用按位或運算子“|”)形成一個型別為regex_constants::match_flag_type:
flag*對匹配的影響說明
match_default預設預設匹配行為。
此常量的值為零**。
match_not_bol非行首第一個字元不被視為“行首”("^"不匹配)。
match_not_eol非行尾最後一個字元不被視為“行尾”("$"不匹配)。
match_not_bow非詞首轉義序列"\b"不匹配為“詞首”。
match_not_eow非詞尾轉義序列"\b"不匹配為“詞尾”。
match_any任意匹配如果可能存在多個匹配,則接受任意一個匹配。
match_not_null非空空序列不匹配。
match_continuous連續表示式必須匹配從第一個字元開始的子序列。
子序列必須從第一個字元開始才能匹配。
match_prev_avail前一個可用第一個字元之前存在一個或多個字元。(match_not_bolmatch_not_bow被忽略)
format_default預設與...相同match_default.
此常量的值為零**。
format_sed此函式忽略。
有關更多資訊,請參閱regex_constants。
format_no_copy
format_first_only
* 這些位掩碼標誌名稱在std::regex_constants名稱空間可用(有關更多詳細資訊,請參閱regex_constants)。
** 值為零的常量如果在設定了其他標誌時會被忽略。
match_flag_type是可用在std::regex_constants名稱空間內定義。
first, last
雙向迭代器,指向用於作為搜尋目標序列的字元範圍的起始和結束位置。使用的範圍是[first,last),其中包括firstlast之間的所有字元,包括first指向的字元,但不包括last指向的字元。
函式模板型別可以是任何指向字元的雙向迭代器
m
一個match_results型別的物件(例如cmatch或smatch),此函式用它來填充有關匹配結果和任何找到的子匹配的資訊。
match_results物件的型別應使用合適的迭代器型別進行例項化,以便遍歷字元序列s(或begin和end之間的字元)。

返回值

true如果“rgx”與目標序列中的子序列匹配。false否則為 false。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// regex_search example
#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)([^ ]*)");   // matches words beginning by "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 subsequence
Regular expression: /\b(sub)([^ ]*)/
The following matches and submatches were found:
subject sub ject
submarine sub marine
subsequence sub sequence


另見