Regex in C++

I’ve missed out on a few things. It was fun to learn (or be reminded?) today that regular expressions have been part of the C++ std library for a while: https://en.cppreference.com/w/cpp/regex

#include <iostream>
#include <regex>
#include <string>

int main(int argc, char **argv)
{
    if (argc != 3) {
        std::cerr << "Usage: ./match_all regex subject\n";
        return 1;
    }
    std::regex re(argv[1]);
    std::string input(argv[2]);

    for (std::sregex_iterator end,
         it = std::sregex_iterator(input.begin(), input.end(), re);
         it != end;
         ++it) {
        std::cout << it->str() << '\n';
    }
}
% g++ --std=c++14 match_all.cpp
% ./a.out '\w+' 'The quick brown fox'
The
quick
brown
fox

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *