std::match_results::str(3)
Hey bestie π welcome to the std::match_results::str(3) page from the C++ Standard Libary manual (yes, spelled exactly like that, no cap). This is the iconic little member that hands you back a match as a string. Letβs get into it π
π NAME π
std::match_results::str β std::match_results::str
yes bestie, the NAME section really said that twice π iconic behavior
β¨ Synopsis β¨
Okay so hereβs the signature, literally:
string_type str( size_type n = 0 ) const; (since C++11)
It returns a string representing the indicated sub-match π«Ά
- If
n == 0, you get a string representing the entire matched expression. the whole thing, main character energy. - If
0 < n && n < size(), you get a string representing the n^th sub-match. sub-match moment. - If
n >= size(), you get a string representing the unmatched match. no cap, thatβs what it says.
The call is equivalent to string_type((*this)[n]); β thatβs the whole vibe π₯
π Parameters π
nβ an integral number specifying which match to return.
πΈ Return value πΈ
Returns a string representing the specified match or sub match. slay
π¦ Example π¦
Run this code, bestie:
// Run this code
#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.str(0) << '\n'
<< "submatch #1: " << sm.str(1) << '\n';
std::regex re2("a(a*)b");
std::regex_search(target, sm, re2);
std::cout << "entire match: " << sm.str(0) << '\n'
<< "submatch #1: " << sm.str(1) << '\n';
}
π Output π
entire match: aaab
submatch #1: a
entire match: aaab
submatch #1: aa
π See also π
operator[]β returns specified sub-match (public member function). iykyk
xoxo, your regex bestie π β source: http://cppreference.com, dated 2024.06.10.
π¦ 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