公共成員函式
<regex>

std::match_results::empty

bool empty() const;
測試物件是否沒有匹配項
返回值true如果match_results物件不包含任何匹配項。

當物件是預設構造的,則此值初始化為true。如果呼叫regex_matchregex_search,其中match_results物件作為引數找到至少一個匹配項,則此值設定為false。如果沒有找到匹配項,則設定為true.

要檢查物件是否已被其中一個函式填充(不區分模式是否成功匹配),可以使用match_results::ready

引數



返回值

true如果物件不包含任何匹配項。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
// match_results::empty
// - using smatch, a standard alias of match_results<string::iterator>
#include <iostream>
#include <string>
#include <regex>

int main ()
{
  using namespace std::regex_constants;

  std::string s ("Subject");
  std::regex e1 ("sub.*");
  std::regex e2 ("sub.*", ECMAScript | icase);

  std::smatch m1,m2;

  std::regex_match ( s, m1, e1 );
  std::regex_match ( s, m2, e2 );

  std::cout << "e1 " << ( m1.empty() ? "did not match" : "matched" ) << std::endl;
  std::cout << "e2 " << ( m2.empty() ? "did not match" : "matched" ) << std::endl;

  return 0;
}

輸出
e1 did not match
e2 matched


另見