regex_replace使用方法

regex_replace

 #include <boost/regex.hpp>

template <class traits, class charT>

basic_string<charT> regex_replace (const basic_string<charT>& s,

                                   const basic_regex<charT, traits>& e,

                                   const basic_string<charT>& fmt,

                                   match_flag_type flags = match_default);   

    Regex算法家族中的第三个算法是 regex_replace. 顾名思义,它是用于执行文本替换的。它在整个输入数据中进行搜索,查找正则表达式的所有匹配。对于表达式的每一个匹配,该算法调用 match_results::format 并输入结果到一个传入函数的输出迭代器。

    我给出了一个例子,将英式拼法的 colour 替换为美式拼法 color. 不使用正则表达式来进行这个拼写更改会非常乏味,也很容易出错。问题是可能存在不同的大小写,而且会有很多单词被影响,如colourize. 要正确地解决这个问题,我们需要把正则表达式分为三个子表达式。

boost::regex reg("(Colo)(u)(r)", boost::regex::icase|boost::regex::perl);

    我们将要去掉的字母u独立开,为了在所有匹配中可以很容易地删掉它。另外,注意到这个正则表达式是大小写无关的,我们要把格式标志 boost::regex::icase 传给 regex 的构造函数。你还要传递你想要设置的其它标志。设置标志时一个常见的错误就是忽略了regex缺省打开的那些标志,如果你没有设置这些标志,它们不会打开,你必须设置所有你要打开的标志。

    调用 regex_replace时,我们要以参数方式提供一个格式化字符串。该格式化字符串决定如何进行替换。在这个格式化字符串中,你可以引用匹配的子表达式,这正是我们想要的。你想保留第一个和第三个匹配的子表达式,而去掉第二个(u)。表达式 $N表示匹配的子表达式, N 为子表达式索引。因此我们的格式化串应该是 "$1$3", 表示替换文本为第一个和第三个子表达式。通过引用匹配的子表达式,我们可以保留匹配文本中的所有大小写,而如果我们用字符串来作替换文本则不能做到这一点。以下是解决这个问题的完整程序。

 

#include <iostream>

#include <string>

#include "boost/regex.hpp"

 

int main()

{

       boost::regex reg("(Colo)(u)(r)", boost::regex::icase|boost::regex::perl);

       std::string s="Colour, colours, color, colourize";

       s=boost::regex_replace(s,reg,"$1$3");

       std::cout << s;

}

 

程序的输出是 "Color, colors, color, colorize". regex_replace 对于这样的文本替换非常有用。

=====================

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:

regularsome regular expressions are RegxyzrRegULarexpressionstring

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 matchesMatching abc doesn’t matchMatching some regular expressions are Regxyzr doesn’t matchMatching RegULarexpressionstring doesn’t matchSearching regular regularSearching abcSearching some regular expressions are Regxyzr regular RegxyzrSearching 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.cppcustomer.cppaccount.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.

Moving Forward

In this article, I’ve barely scratched the surface of what you can do with regular expressions. When you embed sets of parentheses (()) in your patterns, you can locate patterns as well as portions of patterns; then you can use these substrings in replacements, for example.

That’s just the beginning. Regular expressions possess an enormous amount of string-processing power. Entire books have been written on the subject, and I encourage you to check out the sources I mentioned earlier. One exercise you might try is to come up with a regular expression that strips away all the tags from an HTML file, leaving only the plain text. Or one that creates a simpler version of the text for use on small device screens. Whereas somebody not familiar with regular expressions might be inclined to start writing some complex (and possibly bug-ridden) algorithms to solve such problems, they can often be solved with simple regular expressions. Of course, some regular expressions are themselves complex and bug-ridden, but in my experience, the bugs in regular expressions are usually easier to manage, and in the end far less time is spent developing with regular expressions than without.

Regular expressions are an exciting topic and extremely useful. When you master them and put them to use with the help of a good library such as boost::regex, you’ll wonder how you ever lived without them.

posted on 2010-04-20 07:34  cy163  阅读(28788)  评论(0编辑  收藏  举报

导航