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:

[Rr]eg...r

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:

regular
some regular expressions are Regxyzr
RegULarexpressionstring

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:

^[Rr]eg...r$

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:

boost::match_results<std::string::const_iterator>

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
 matches
Matching abc
 doesn’t match
Matching some regular expressions are Regxyzr
 doesn’t match
Matching RegULarexpressionstring
 doesn’t match
Searching regular
 regular
Searching abc
Searching some regular expressions are Regxyzr
 regular
 Regxyzr
Searching RegULarexpressionstring
 RegULar

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[])
{
 test1();
 cout << endl;
 test2();
}

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:

\w*\.cpp

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:

\\w*\\.cpp

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:

$&\n

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:

main.cpp
customer.cpp
account.cpp

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