How do I use std::regex for pattern matching?

Understanding std::regex in C++

keywords: regex, pattern matching, C++, std::regex

description: This article explains how to use std::regex for pattern matching in C++.


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

int main() {
    std::string text = "C++ is a powerful programming language.";
    std::regex pattern("C\\+\\+");

    if (std::regex_search(text, pattern)) {
        std::cout << "Pattern found!" << std::endl;
    } else {
        std::cout << "Pattern not found!" << std::endl;
    }

    return 0;
}
    

keywords: regex pattern matching C++ std::regex