std::match_...:operator[](3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 > 0 and n < size(), it returns a reference to the std::sub_match representing the part of the target sequence that was matched by the n^th captured marked subexpression. πŸ”₯
  • If n == 0, it returns a reference to the std::sub_match representing 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 a std::sub_match representing 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