Home > Articles > Programming > C/C++

  • Print
  • + Share This
Like this article? We recommend

Programming Regular Expressions in C++

With a bit of regular expression knowledge, we can write some code to try out some of these examples. The library I use here is the Boost library. If you’ve ready any of my other articles, you know that I’m a big fan of the Boost library, for many reasons. First, the library is solid and useful. Second, because the creation of the library is headed up by the members of the C++ standards committee, many of the classes have a good shot of making it into the next official C++ standard. Thus, for these examples, I use the Boost Regex library. (You can download the complete Boost library and learn more about it at the Boost.org site.)

Matching Versus Searching

In the world of regular expressions, you’ll often find that you need two common tasks: matching and searching. That is, given a search pattern and a string, you might need to determine whether the string perfectly matches the pattern. Or you might need to determine whether the string contains the pattern. A couple examples will help explain this.

Consider this pattern:


The brackets ([]) constitute an "or" situation; in this case, the first character can be either r or R.

Now let’s test the following strings against this pattern:

some regular expressions are Regxyzr

Does the first string match the pattern? Yes, it does. The pattern says that a string must start with either r or R, followed by eg and then any three characters, followed by r. We have exactly that with the first string. Further, does the string contain the pattern? Sure it does. It definitely contains a set of characters that match the pattern. In other words, if I search the string for the pattern, I’ll find the pattern.

What about the second string? Does it exactly match the pattern? No. It doesn’t start with an r or R followed by eg, and so on. But what if I search the string—will I find the pattern? Yes. In fact, I’ll find the pattern in two different places.

Now consider the third string. If you search the string, you’ll find the pattern at the beginning of the string. But what about matching? Whether this string matches the expression depends on how you code your regular expression system, as well as how you tell the system to use your regular expression pattern. Regular expression systems can be told that a "match" requires the beginning of the string to match the expression, and anything following is ignored; in that case, the third string will match the pattern. Or a system can be told that the string must not have any characters after the pattern, in which case our third string will not match the pattern. The default rule with the Boost library I’ll be demonstrating shortly is to not consider such a string a match.

One way to be precise regarding strings that extend beyond the pattern is to use anchors. An anchor is a pattern character that represents either "start of string" or "end of string." This arrangement gives you greater control over how the pattern is interpreted, leaving no room for ambiguity. Here’s the previous expression, rewritten with anchors:


This pattern starts with a start-of-string character (^), which means that the pattern must exist at the start of the string. The pattern ends with the end-of-string character ($), which means that the pattern must not be followed by any characters. With this pattern, the first string, regular, definitely matches. The second string doesn’t match. And the third string doesn’t match because the final r in the pattern must end the string.

Trying It Out

Since searching and matching are so common, the Boost::Regex library has functions for both. The following code demonstrates both, using the preceding test strings as well as another, abc.

#include <iostream>
#include <boost/regex.hpp>

using namespace std;

void testMatch(const boost::regex &ex, const string st) {
 cout << "Matching " << st << endl;
 if (boost::regex_match(st, ex)) {
  cout << " matches" << endl;
 else {
  cout << " doesn’t match" << endl;

void testSearch(const boost::regex &ex, const string st) {
 cout << "Searching " << st << endl;
 string::const_iterator start, end;
 start = st.begin();
 end = st.end();
 boost::match_results<std::string::const_iterator> what;
 boost::match_flag_type flags = boost::match_default;
 while(boost::regex_search(start, end, what, ex, flags))
  cout << " " << what.str() << endl;
  start = what[0].second;

int main(int argc, char *argv[])
 static const boost::regex ex("[Rr]eg...r");
 testMatch(ex, "regular");
 testMatch(ex, "abc");
 testMatch(ex, "some regular expressions are Regxyzr");
 testMatch(ex, "RegULarexpressionstring");
 testSearch(ex, "regular");
 testSearch(ex, "abc");
 testSearch(ex, "some regular expressions are Regxyzr");
 testSearch(ex, "RegULarexpressionstring");
 return 0;

To make the example clear, I created two sample functions, testMatch and testSearch. The testMatch function demonstrates how to test for a match, whereas testSearch demonstrates searching. Each takes as parameters a regular expression object and a string being tested.

Before exploring the testMatch and testSearch functions, however, take a look at the main function, and notice how I created my regular expression pattern: I simply created a new object of type boost::regex, passing my pattern string into the constructor. Easy enough. And that’s what I pass to my sample functions.

Now look at the testMatch function, and notice how I test whether the passed-in string matches against the regular expression pattern:

if (boost::regex_match(st, ex)) {

The boost::regex_match function is a template function that takes as a parameter the string being tested, followed by the regular expression object. That’s it! This function returns a Boolean value if the string matches the expression. Pretty simple.

Searching is a bit more complex, but still shouldn’t cause a loss of hair and an increase in blood pressure. The boost::regex_search function needs an iterator provided by the string you’re testing.

start = st.begin();
end = st.end();

The boost::regex_search function returns a Boolean value denoting whether the function found another instance of the pattern in the string. Thus, your best bet is to use boost::regex_search within a while loop.

You need to pass the two string iterators to the regex_search function, followed by an object that will contain the results of the search (more on that shortly), followed by the regular expression object, and finally a set of flags specifying how to perform the search.

The search results are placed in a variable of template type boost::match_results. The template parameter is the same as the iterator; thus, the full type for the result is as follows:


Inside the while loop, where regex_search found an instance of the pattern, you can access the current result through the results variable, which I called what in the example code. The what variable includes a function, str(), which returns the substring that matched the pattern.

Finally, to move forward in the loop (and avoid an infinite loop!), you need to advance the iterator manually. The what variable works like an array, and contains instances of a class called sub_match. To advance the pointer, you access the first element, what[0], and from there you grab the element called second:

start = what[0].second;

This technique will advance the iterator to the next position in the string.

Here’s the output for the preceding listing:

Matching regular
Matching abc
 doesn’t match
Matching some regular expressions are Regxyzr
 doesn’t match
Matching RegULarexpressionstring
 doesn’t match
Searching regular
Searching abc
Searching some regular expressions are Regxyzr
Searching RegULarexpressionstring

Replacing Strings

Another common use of regular expressions is in finding substrings that match a pattern and replacing those strings with another string. The following code demonstrates this technique; I’m demonstrating two different approaches in separate sample functions, test1 and test2.

#include <iostream>
#include <sstream>
#include <boost/regex.hpp>
#include <iterator>

using namespace std;

void test1() {
 static const boost::regex ex("[Ss].{0,1}e");
 string initial = "She sells sea shells by the sea shore";
 string fmt = "-$&-";
 string result = boost::regex_replace(initial, ex, fmt);
 cout << result << endl;

void test2() {
 static const boost::regex ex("\\w*\\.cpp");
 string initial = "main.cpp main.h customer.cpp customer.h account.cpp account.h";
 string fmt = "$&\n";
 std::ostringstream res(std::ios::out);
 std::ostream_iterator<char> ores(res);
 boost::regex_replace(ores, initial.begin(), initial.end(),
  ex, fmt, boost::match_default | boost::format_no_copy);
 cout << res.str() << endl;

int main(int argc, char *argv[])
 cout << endl;

In the test1 function, I’m creating a regular expression that matches either a capital or lowercase S, followed by one or zero single characters followed by e. In other words, I’m matching Se and se, possibly with a single character between the two letters.

I then create the string that I’m going to replace; I give this string the variable name initial. After that, I create a format string that specifies what I want to do with the substrings that match. Just to demonstrate what’s happening, I’m simply copying the matched substring, but surrounding it with two minus (-) signs. The $& refers to the matched substring; you can find the whole list here. Thus, if the matched substring is She, then the string will be replaced with -She-.

This example isn’t particularly interesting, but it demonstrates a simple replacement. The call to do the replacing is regex_replace; I pass as parameters the initial string, the regular expression, and the format string. The function returns a string, which I save in the variable called result.

My input string, then, is this:

She sells sea shells by the sea shore

The regular expression will match She, se, se, she, sea. The format string says to replace each of these with the itself surrounded by minus signs: -She-, -se-, -se-, -she-, and –sea-, resulting in a final string:

-She- -se-lls -se-a -she-lls by the -se-a shore

The second sample function, test2, is a bit more useful example. It takes an input string consisting of a bunch of filenames, in this case various .cpp and .h files. I’m going to search the string for .cpp filenames. Here’s the regular expression I’m using:


The \w in this expression works much like the . character except that, instead of matching just any character, it matches only characters that are letters, digits, or underscores. The * means that I’ll match multiple characters. After the set of letters, numbers, and underscores, I’m matching a period. Since the period is already a special character, I need to inform the regular expression engine that I really do want a period. Normally a period is a wildcard character matching anything; to find a real period, I precede it with a backslash to get the \. pattern. Next, I follow the pattern with cpp.

To recap, I’ll match anything that contains a string of characters consisting of letters, numbers, and underscores, followed by a period and then cpp.

Notice, however, that in the code I used double backslashes for the regular expression:


The reason is that the regular expression requires single slashes, but to get single slashes into a string in C++, you have to use double slashes.

In this code, I’ll replace each matched substring with this:


As before, the $& denotes the matched substring. The \n denotes a newline.

But what about the parts of the string that don’t match? In the previous example, they were just left intact, as you can see in the output:

-She- -se-lls -se-a -she-lls by the -se-a shore

With the present example, however, I want the unmatched parts to be thrown out. I don’t want them to appear in the modified string. To make that happen, I set some options with the regex_replace function. But to use those options, I need to use another form of the function. This form takes as parameters an iterator into an output stream that will receive the modified string (in contrast to the previous form, which simply returned a string); an iterator denoting the start and end of the string being searched; the regular expression object; the format string; and finally the options.

To set up the receiving string, I need to use a stream; thus, I use a stringstream object. Then I create an iterator for the object:

std::ostringstream res(std::ios::out);
std::ostream_iterator<char> ores(res);

I called my iterator object ores, and that’s what I pass as the first parameter to the regex_replace function.

When you compile and run the code, here’s the output you’ll see from the test2 function:


This output is one string with three substrings, each followed by a newline \n character. Thus, it worked: With a single regular expression, we were able to go through a list of filenames separated by spaces all in a single string, and get only the .cpp files. We could have done anything we wanted; for example, we could have used a space instead of \n in the format string so the filenames would remain on a single line. It’s up to you and the requirements of your project.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020