function template
<regex>

std::regex_match

C-strings (1)
template <class charT, class traits>  bool regex_match (const charT* s, const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
strings (2)
template <class ST, class SA, char charT, class traits>  bool regex_match (const basic_string<charT,ST,SA>& s,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
ranges (3)
template <class BidirectionalIterator, class charT, class traits>  bool regex_match (BidirectionalIterator first, BidirectionalIterator last,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
with match_results (4,5,6)
template <class charT, class Alloc, class traits>  bool regex_match (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_match (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_match (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-strings (1)
template <class charT, class traits>  bool regex_match (const charT* s, const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
strings (2)
template <class ST, class SA, char charT, class traits>  bool regex_match (const basic_string<charT,ST,SA>& s,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
ranges (3)
template <class BidirectionalIterator, class charT, class traits>  bool regex_match (BidirectionalIterator first, BidirectionalIterator last,          const basic_regex<charT,traits>& rgx,          regex_constants::match_flag_type flags = regex_constants::match_default);
with match_results (4,5,6)
template <class charT, class Alloc, class traits>  bool regex_match (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_match (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_match (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);
moving string (deleted) (7)
template <class ST, class SA, class Alloc, class charT, class traits>  bool regex_match (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;
Match sequence
返回正則表示式rgx是否與目標序列匹配。目標序列根據所使用的版本,可以是s,也可以是firstlast之間的字元序列。

版本456分別與123相同,只是它們接受一個match_results型別的物件作為引數,該物件被填充了匹配結果的資訊。

刪除帶有移動字串 (7)的簽名會阻止使用臨時string物件進行構造。

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

為了使此函式返回true(即,在匹配前後沒有任何額外的字元),整個目標序列必須與正則表示式匹配。對於一個在匹配僅是序列的一部分時返回的函式,請參閱 regex_searchtruewhen the match is only part of the sequence, see regex_search.

引數

s
一個包含目標序列(主題)的字串,正則表示式將與之匹配。
rgx
一個basic_regex物件(模式)進行匹配。
標誌
用於控制rgx如何匹配的標誌。
可以將一個或多個這些常量組合(使用按位或運算子 |)形成一個型別為regex_constants::match_flag_type:
flag*effects on match說明
match_defaultDefault預設匹配行為。
此常量的值為零**。
match_not_bolNot Beginning-Of-Line第一個字元不被視為行首"^"不匹配)。
match_not_eolNot End-Of-Line最後一個字元不被視為行尾"$"不匹配)。
match_not_bowNot Beginning-Of-Word轉義序列"\b"不匹配為單詞開頭
match_not_eowNot End-Of-Word轉義序列"\b"不匹配為單詞結尾
match_anyAny match如果可能存在多個匹配,則任何一個匹配都可以接受。
match_not_nullNot null空序列不匹配。
match_continuousContinuous表示式必須匹配從第一個字元開始的子序列。
子序列必須從第一個字元開始才能匹配。
match_prev_availPrevious Available第一個字元之前存在一個或多個字元。(match_not_bolmatch_not_bow被忽略)
format_defaultDefaultSame asmatch_default.
此常量的值為零**。
format_sed此函式忽略。
有關更多資訊,請參閱 regex_constants
format_no_copy
format_first_only
* 這些位掩碼標誌名稱可在std::regex_constants名稱空間中使用(有關更多詳細資訊,請參閱 regex_constants)。
** 值為零的常量在設定了其他標誌時會被忽略。
match_flag_type是可在std::regex_constants名稱空間內定義。
first, last
Bidirectional iterators 指向用作匹配目標序列的字元範圍的起始和結束位置。使用的範圍是[first,last),其中包括firstlast之間的所有字元,包括first指向的字元,但不包括last指向的字元。
函式模板型別可以是任何指向字元的雙向迭代器
m
一個match_results型別的物件(例如cmatchsmatch),該函式將用匹配結果和任何找到的子匹配的資訊來填充它。
match_results物件的型別應使用適當的迭代器型別進行例項化,以迭代遍歷字元序列s(或beginend之間的字元)。

返回值

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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// regex_match example
#include <iostream>
#include <string>
#include <regex>

int main ()
{

  if (std::regex_match ("subject", std::regex("(sub)(.*)") ))
    std::cout << "string literal matched\n";

  const char cstr[] = "subject";
  std::string s ("subject");
  std::regex e ("(sub)(.*)");

  if (std::regex_match (s,e))
    std::cout << "string object matched\n";

  if ( std::regex_match ( s.begin(), s.end(), e ) )
    std::cout << "range matched\n";

  std::cmatch cm;    // same as std::match_results<const char*> cm;
  std::regex_match (cstr,cm,e);
  std::cout << "string literal with " << cm.size() << " matches\n";

  std::smatch sm;    // same as std::match_results<string::const_iterator> sm;
  std::regex_match (s,sm,e);
  std::cout << "string object with " << sm.size() << " matches\n";

  std::regex_match ( s.cbegin(), s.cend(), sm, e);
  std::cout << "range with " << sm.size() << " matches\n";

  // using explicit flags:
  std::regex_match ( cstr, cm, e, std::regex_constants::match_default );

  std::cout << "the matches were: ";
  for (unsigned i=0; i<cm.size(); ++i) {
    std::cout << "[" << cm[i] << "] ";
  }

  std::cout << std::endl;

  return 0;
}

輸出
string literal matched
string object matched
range matched
string literal with 3 matches
string object with 3 matches
range with 3 matches
the matches were: [subject] [sub] [ject]


另見