std::match_...:operator[](3)
π
std::match_results::operator[] β C++ Standard Library manual page β¨
okay bestie, this is the C++ Standard Library manual page for std::match_results::operator[], and it is literally the member that gives you a std::sub_match reference when you index into your match results. icon behavior, regex girlie. π
π¦ NAME π¦
std::match_results::operator[] β yes, that is the whole name, and no, we are not skipping the intro. periodt.
β¨ Synopsis β¨
const_reference operator[]( size_type n ) const; (since C++11)
Here's the vibe check on what each index gets you:
- If
n > 0andn < size(), it returns a reference to thestd::sub_matchrepresenting the part of the target sequence that was matched by the n^th captured marked subexpression. π₯ - If
n == 0, it returns a reference to thestd::sub_matchrepresenting the part of the target sequence matched by the entire matched regular expression. main character match energy. - If
n >= size(), it returns a reference to astd::sub_matchrepresenting an unmatched sub-expression β which is an empty subrange of the target sequence. π - The behavior is undefined unless
ready() == true.
π¨ bestie, seriously: the behavior is undefined unless ready() == true. That is not a cute little edge case, that is a hard no. π
πΈ Parameters πΈ
nβ an integral number specifying which match to return. it's giving "which one, bestie?" π
π«Ά Return value π«Ά
A reference to the std::sub_match representing the specified matched subrange within the target sequence. slay. β¨
π Example π
#include <iostream>
#include <regex>
#include <string>
int main()
{
std::string target("baaaby");
std::smatch sm;
std::regex re1("a(a)*b");
std::regex_search(target, sm, re1);
std::cout << "entire match: " << sm[0] << '\n'
<< "submatch #1: " << sm[1] << '\n';
std::regex re2("a(a*)b");
std::regex_search(target, sm, re2);
std::cout << "entire match: " << sm[0] << '\n'
<< "submatch #1: " << sm[1] << '\n';
}
π Output π
entire match: aaab
submatch #1: a
entire match: aaab
submatch #1: aa
π See also π
strβ returns the sequence of characters for the particular sub-match (public member function). the supporting character that also ate. π
mwah π β this page is serving C++ Standard Library manual realness from http://cppreference.com, dated 2024.06.10. go off, regex bestie. π§ββοΈβ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms