當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ match_results size()用法及代碼示例


match_results size()是C++中的內置函數,它返回match_result對象中的匹配數和sub-matches。

用法:

smatch_name.size()

Note: smatch_name is an object of match_results class.

參數:該函數不接受任何參數。


返回值:它返回match_result對象中的匹配數。

注意:第一個元素始終包含整個正則表達式匹配項,而其他元素則包含特定的捕獲組

以下示例程序旨在說明上述函數。

示例1:

// CPP program to illustrate 
// match_results size() in C++ 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    string s("Geeksforgeeks"); 
    regex re("(Geeks)(.*)"); 
  
    smatch match; 
  
    // Function call to find match 
    regex_match(s, match, re); 
  
    cout << "match size is " << match.size() << endl; 
  
    return 0; 
}
輸出:
match size is 3

示例2:

// CPP program to illustrate 
// match_results size() in C++ 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    string s("Geeksforgeeks"); 
    regex re("(Geeks)(.*)"); 
  
    smatch match; 
  
    // Function call to find matches 
    regex_match(s, match, re); 
  
    cout << "match size is " << match.size() << endl; 
  
    for (int i = 0; i < match.size(); i++) { 
        cout << "match " << i << " has a length of "
             << match.length(i) << endl; 
    } 
    return 0; 
}
輸出:
match size is 3
match 0 has a length of 13
match 1 has a length of 5
match 2 has a length of 8


相關用法


注:本文由純淨天空篩選整理自Harsha_Mogali大神的英文原創作品 match_results size() in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。