Ticket #6493: t.cpp

File t.cpp, 759 bytes (added by little_bird@…, 11 years ago)

An example in using regex

Line 
1#include <iostream>
2#include "boost/regex.hpp"
3
4void boostTest()
5{
6 std::string toTest = "abc<NPC><NPC>12345</NPC>1</NPC>2<NPC>3</NPC>";
7 std::string regstr = "<(?<HtmlTag>NPC)>((?<Nested><\k<HtmlTag>>)|</\k<HtmlTag>>(?<-Nested>)|.*?)*</\k<HtmlTag>>";
8 boost::smatch what;
9 boost::regex expr(regstr);
10 std::string::const_iterator start = toTest.begin();
11 std::string::const_iterator end = toTest.end();
12 int pos = 0;
13 while (boost::regex_search(start, end, what, expr))
14 {
15 std::cout << "Match group " << ++pos << ":" << std::endl;
16 for (int i = 0; i < what.size(); i++)
17 {
18 std::string msg(what[i].first, what[i].second);
19 std::cout << i << ": " << msg.c_str() << std::endl;
20 }
21 start = what[0].second;
22 }
23}
24
25int main()
26{
27 boostTest();
28 return 0;
29}