名稱空間
變體
操作

std::sub_match<BidirIt>::length

來自 cppreference.com
< cpp‎ | regex‎ | sub_match
 
 
 
正則表示式庫
(C++11)
演算法
迭代器
異常
特性
常量
(C++11)
正則表示式語法
 
std::sub_match
成員函式
sub_match::length
非成員函式
(直至 C++20)(直至 C++20)(直至 C++20)(直至 C++20)(直至 C++20)(C++20 起)
 
difference_type length() const;

返回匹配中的字元數。

[編輯] 返回值

若匹配合法,則為 std::distance(first, second),否則為 0

[編輯] 複雜度

常數時間。

[編輯] 示例

#include <iostream>
#include <regex>
#include <string>
 
int main()
{
    std::string sentence{"Quick red fox jumped over a lazy cow."};
    const std::regex re{"([A-z]+) ([a-z]+) ([a-z]+) ([a-z]+)"};
    std::smatch words;
    std::regex_search(sentence, words, re);
    for (const auto& m : words)
        std::cout << '[' << m << "], length = " << m.length() << '\n';
}

輸出

[Quick red fox jumped], length = 20
[Quick], length = 5
[red], length = 3
[fox], length = 3
[jumped], length = 6