1 | #include <iostream>
|
---|
2 | #include <string>
|
---|
3 |
|
---|
4 | #include <boost/regex.hpp>
|
---|
5 | #include <boost/format.hpp>
|
---|
6 |
|
---|
7 | // g++ -o regex-bug{,.cpp} -lboost_regex -Wall -pedantic
|
---|
8 | //
|
---|
9 | // tests case :
|
---|
10 | // echo " toto" | regex '[a-z]*' > result_0
|
---|
11 | // echo " toto" | regex ' [a-z]*' > result_1
|
---|
12 | //
|
---|
13 | //
|
---|
14 | //
|
---|
15 |
|
---|
16 | char const * c_decorate_string = "% -12s (%s)" ;
|
---|
17 | char const * c_decorate_matched = " [% -3d,% -3d] (%s)" ;
|
---|
18 |
|
---|
19 | int main(int argc, char * argv[])
|
---|
20 | {
|
---|
21 | using namespace std ;
|
---|
22 | using namespace boost ;
|
---|
23 |
|
---|
24 | cout << format(c_decorate_string) % "seeked" % argv[1] << endl ;
|
---|
25 |
|
---|
26 | regex re(argv[1]) ;
|
---|
27 | string extract ;
|
---|
28 | getline(cin, extract) ;
|
---|
29 |
|
---|
30 | cout << format(c_decorate_string) % "input" % extract << endl ;
|
---|
31 |
|
---|
32 | if(extract.length() > 0)
|
---|
33 | {
|
---|
34 | string const & c_extract = extract ;
|
---|
35 |
|
---|
36 | string::const_iterator start = c_extract.begin() ;
|
---|
37 | string::const_iterator end = c_extract.end() ;
|
---|
38 |
|
---|
39 |
|
---|
40 | smatch matches ;
|
---|
41 | match_flag_type flags = match_default ;
|
---|
42 | unsigned counter = 0 ;
|
---|
43 | while(regex_search(start, end, matches, re, flags))
|
---|
44 | {
|
---|
45 | cout << format(c_decorate_string) % "current" % std::string(start, end) << endl ;
|
---|
46 |
|
---|
47 | for(unsigned i = 0 ; i < matches.size() ; i++)
|
---|
48 | cout << format(c_decorate_matched)
|
---|
49 | % counter % i
|
---|
50 | % std::string(matches[i].first, matches[i].second) << endl ;
|
---|
51 |
|
---|
52 | cout << format(c_decorate_string)
|
---|
53 | % "suffix"
|
---|
54 | % std::string(matches.suffix().first, matches.suffix().second) << endl ;
|
---|
55 |
|
---|
56 | cout << format(c_decorate_string)
|
---|
57 | % "prefix"
|
---|
58 | % std::string(matches.prefix().first, matches.prefix().second) << endl ;
|
---|
59 |
|
---|
60 | counter++ ;
|
---|
61 | flags |= match_prev_avail ;
|
---|
62 | flags |= match_not_bob ;
|
---|
63 |
|
---|
64 | if(matches[0].first == matches[0].second)
|
---|
65 | throw "If the string is null, it means regex_search has to be false !" ;
|
---|
66 | else
|
---|
67 | start = matches[0].second ;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | return 0 ;
|
---|
72 | }
|
---|
73 |
|
---|