I could not find the reference of std::regex
library. I did some google searches and found some tutorials, but they're all brief and short. I couldn't figure out how to tokenize a string using regex.
Could anyone give me a hint how to start?
A video tutorial on STL regular expressions.
#include <regex>
#include <iostream>
#include <string>
void ifIWantASpecificMatch ()
{
const std::string input = "Hello World";
const std::regex regex("(.*) (.*)");
std::smatch match; // std::match_result ready templated for std::string
if (std::regex_match(input, match, regex)) {
std::cout << "full match " << match[0] << std::endl;
std::cout << "first group " << match[1] <<
" beginning at: " << match[1].first - input.begin() << std::endl;
std::cout << "second group " << match[2] <<
" beginning at: " << match[2].first - input.begin() << std::endl;
}
}
void ifIWantToIterateAllMatches ()
{
const std::string input = "I want to get all the o's";
const std::regex regex("o");
std::smatch match;
for (std::sregex_iterator it(input.begin(), input.end(), regex), end; it != end; ++it) {
std::cout << "Found at: " << it->position() << std::endl;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With