17.3.1. Using the Regular Expression Library

As a fairly simple example, we’ll look for words that violate a well-known spelling rule of thumb, “i before e except after c”:

// find the characters ei that follow a character other than cstring pattern("[^c]ei");// we want the whole word in which our pattern appearspattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";regex r(pattern); // construct a regex to find patternsmatch results;   // define an object to hold the results of a search// define a string that has text that does and doesn't match patternstring test_str = "receipt freind theif receive";// use r to find a match to pattern in test_strif (regex_search(test_str, results, r)) // if there is a match    cout << results.str() << endl;      // ...

Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.