ing](http://en.cppreference.com/w/cpp/string/basic_string) s = "Some people, when confronted with a problem, think "
            "\"I know, I'll use regular expressions.\" "
            "Now they have two problems.";
     
        [std::regex](http://en.cppreference.com/w/cpp/regex/basic_regex) self_regex("REGULAR EXPRESSIONS",
            [std::regex_constants::ECMAScript](http://en.cppreference.com/w/cpp/regex/syntax_option_type) | [std::regex_constants::icase](http://en.cppreference.com/w/cpp/regex/syntax_option_type));
        if ([std::regex_search](http://en.cppreference.com/w/cpp/regex/regex_search)(s, self_regex))
            [std::cout](http://en.cppreference.com/w/cpp/io/cout) << "Text contains the phrase 'regular expressions'\n";
     
        [std::regex](http://en.cppreference.com/w/cpp/regex/basic_regex) word_regex("(\\w+)");
        auto words_begin = 
            [std::sregex_iterator](http://en.cppreference.com/w/cpp/regex/regex_iterator)(s.begin(), s.end(), word_regex);
        auto words_end = [std::sregex_iterator](http://en.cppreference.com/w/cpp/regex/regex_iterator)();
     
        [std::cout](http://en.cppreference.com/w/cpp/io/cout) << "Found "
                  << [std::distance](http://en.cppreference.com/w/cpp/iterator/distance)(words_begin, words_end)
                  << " words\n";
     
        const int N = 6;
        [std::cout](http://en.cppreference.com/w/cpp/io/cout) << "Words longer than " << N << " characters:\n";
        for ([std::sregex_iterator](http://en.cppreference.com/w/cpp/regex/regex_iterator) i = words_begin; i != words_end; ++i)
        {
            [std::smatch](http://en.cppreference.com/w/cpp/regex/match_results) match = *i;
            [std::string](http://en.cppreference.com/w/cpp/string/basic_string) match_str = match.str();
            if (match_str.size() > N)
                [std::cout](http://en.cppreference.com/w/cpp/io/cout) << "  " << match_str << '\n';
        }
     
        [std::regex](http://en.cppreference.com/w/cpp/regex/basic_regex) long_word_regex("(\\w{7,})");
        [std::string](http://en.cppreference.com/w/cpp/string/basic_string) new_s = [std::regex_replace](http://en.cppreference.com/w/cpp/regex/regex_replace)(s, long_word_regex, "[$&]");
        [std::cout](http://en.cppreference.com/w/cpp/io/cout) << new_s << '\n';
    }

Output:

    
    
    Text contains the phrase 'regular expressions'
    Found 20 words
    Words longer than 6 characters:
      confronted
      problem
      regular
      expressions
      problems
    Some people, when [confronted] with a [problem], think 
    "I know, I'll use [regular] [expressions]." Now they have two [problems].

Retrieved from " [
https://en.cppreference.com/mwiki/index.php?title=cpp/regex&oldid=169552
](https://en.cppreference.com/mwiki/index.php?title=cpp/regex&oldid=169552) "

#####  Navigation

  * [ Support us ](http://www.cppreference.com/support)
  * [ Recent changes ](/w/Special:RecentChanges "A list of recent changes in the wiki \[r\]")
  * [ FAQ ](/w/Cppreference:FAQ)
  * [ Offline version ](/w/Cppreference:Archives)

#####  Toolbox

  * [ What links here ](/w/Special:WhatLinksHere/cpp/regex "A list of all wiki pages that link here \[j\]")
  * [ Related changes ](/w/Special:RecentChangesLinked/cpp/regex "Recent changes in pages linked from this page \[k\]")
  * [ Upload file ](http://upload.cppreference.com/w/Special:Upload "Upload files \[u\]")
  * [ Special pages ](/w/Special:SpecialPages "A list of all special pages \[q\]")
  * [ Printable version ](/mwiki/index.php?title=cpp/regex&printable=yes "Printable version of this page \[p\]")
  * [ Permanent link ](/mwiki/index.php?title=cpp/regex&oldid=169552 "Permanent link to this revision of the page")
  * [ Page information ](/mwiki/index.php?title=cpp/regex&action=info)

  * In other languages 

  * [ العربية ](http://ar.cppreference.com/w/cpp/regex "cpp/regex")
  * [ Deutsch ](http://de.cppreference.com/w/cpp/regex "cpp/regex")
  * [ Esp