Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/xpressive/doc/examples.qbk @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 7.3 KB
Line 
1[section Examples]
2
3Below you can find six complete sample programs.
4\n
5
6----
7
8[h4 See if a whole string matches a regex]
9
10This is the example from the Introduction. It is reproduced here for your convenience.
11
12    #include <iostream>
13    #include <boost/xpressive/xpressive.hpp>
14
15    using namespace boost::xpressive;
16
17    int main()
18    {
19        std::string hello( "hello world!" );
20
21        sregex rex = sregex::compile( "(\\w+) (\\w+)!" );
22        smatch what;
23
24        if( regex_match( hello, what, rex ) )
25        {
26            std::cout << what[0] << '\n'; // whole match
27            std::cout << what[1] << '\n'; // first capture
28            std::cout << what[2] << '\n'; // second capture
29        }
30
31        return 0;
32    }
33
34This program outputs the following:
35
36[pre
37hello world!
38hello
39world
40]
41\n
42[link boost_xpressive.user_s_guide.examples top]
43
44----
45
46[h4 See if a string contains a sub-string that matches a regex]
47
48Notice in this example how we use custom `mark_tag`s to make the pattern more readable.
49We can use the `mark_tag`s later to index into the _match_results_.
50
51    #include <iostream>
52    #include <boost/xpressive/xpressive.hpp>
53
54    using namespace boost::xpressive;
55
56    int main()
57    {
58        char const *str = "I was born on 5/30/1973 at 7am.";
59
60        // define some custom mark_tags with names more meaningful than s1, s2, etc.
61        mark_tag day(1), month(2), year(3), delim(4);
62
63        // this regex finds a date
64        cregex date = (month= repeat<1,2>(_d))           // find the month ...
65                   >> (delim= (set= '/','-'))            // followed by a delimiter ...
66                   >> (day=   repeat<1,2>(_d)) >> delim  // and a day followed by the same delimiter ...
67                   >> (year=  repeat<1,2>(_d >> _d));    // and the year.
68
69        cmatch what;
70
71        if( regex_search( str, what, date ) )
72        {
73            std::cout << what[0]     << '\n'; // whole match
74            std::cout << what[day]   << '\n'; // the day
75            std::cout << what[month] << '\n'; // the month
76            std::cout << what[year]  << '\n'; // the year
77            std::cout << what[delim] << '\n'; // the delimiter
78        }
79
80        return 0;
81    }
82
83This program outputs the following:
84
85[pre
865/30/1973
8730
885
891973
90/
91]
92\n
93[link boost_xpressive.user_s_guide.examples top]
94
95----
96
97[h4 Replace all sub-strings that match a regex]
98
99The following program finds dates in a string and marks them up with pseudo-HTML.
100
101    #include <iostream>
102    #include <boost/xpressive/xpressive.hpp>
103
104    using namespace boost::xpressive;
105
106    int main()
107    {
108        std::string str( "I was born on 5/30/1973 at 7am." );
109
110        // essentially the same regex as in the previous example, but using a dynamic regex
111        sregex date = sregex::compile( "(\\d{1,2})([/-])(\\d{1,2})\\2((?:\\d{2}){1,2})" );
112
113        // As in Perl, $& is a reference to the sub-string that matched the regex
114        std::string format( "<date>$&</date>" );
115
116        str = regex_replace( str, date, format );
117        std::cout << str << '\n';
118
119        return 0;
120    }
121
122This program outputs the following:
123
124[pre
125I was born on <date>5/30/1973</date> at 7am.
126]
127\n
128[link boost_xpressive.user_s_guide.examples top]
129
130----
131
132[h4 Find all the sub-strings that match a regex and step through them one at a time]
133
134The following program finds the words in a wide-character string.
135It uses `wsregex_iterator`. Notice that dereferencing a `wsregex_iterator`
136yields a `wsmatch` object.
137
138    #include <iostream>
139    #include <boost/xpressive/xpressive.hpp>
140
141    using namespace boost::xpressive;
142
143    int main()
144    {
145        std::wstring str( L"This is his face." );
146
147        // find a whole word
148        wsregex token = +alnum;
149
150        wsregex_iterator cur( str.begin(), str.end(), token );
151        wsregex_iterator end;
152
153        for( ; cur != end; ++cur )
154        {
155            wsmatch const &what = *cur;
156            std::wcout << what[0] << L'\n';
157        }
158
159        return 0;
160    }
161
162This program outputs the following:
163
164[pre
165This
166is
167his
168face
169]
170\n
171[link boost_xpressive.user_s_guide.examples top]
172
173----
174
175[h4 Split a string into tokens that each match a regex]
176
177The following program finds race times in a string and displays first
178the minutes and then the seconds. It uses _regex_token_iterator_.
179
180    #include <iostream>
181    #include <boost/xpressive/xpressive.hpp>
182
183    using namespace boost::xpressive;
184
185    int main()
186    {
187        std::string str( "Eric: 4:40, Karl: 3:35, Francesca: 2:32" );
188
189        // find a race time
190        sregex time = sregex::compile( "(\\d):(\\d\\d)" );
191
192        // for each match, the token iterator should first take the value of
193        // the first marked sub-expression followed by the value of the second
194        // marked sub-expression
195        int const subs[] = { 1, 2 };
196
197        sregex_token_iterator cur( str.begin(), str.end(), time, subs );
198        sregex_token_iterator end;
199
200        for( ; cur != end; ++cur )
201        {
202            std::cout << *cur << '\n';
203        }
204
205        return 0;
206    }
207
208This program outputs the following:
209
210[pre
2114
21240
2133
21435
2152
21632
217]
218\n
219[link boost_xpressive.user_s_guide.examples top]
220
221----
222
223[h4 Split a string using a regex as a delimiter]
224
225The following program takes some text that has been marked up with html and strips
226out the mark-up. It uses a regex that matches an HTML tag and a _regex_token_iterator_
227that returns the parts of the string that do ['not] match the regex.
228
229    #include <iostream>
230    #include <boost/xpressive/xpressive.hpp>
231
232    using namespace boost::xpressive;
233
234    int main()
235    {
236        std::string str( "Now <bold>is the time <i>for all good men</i> to come to the aid of their</bold> country." );
237
238        // find a HTML tag
239        sregex html = '<' >> optional('/') >> +_w >> '>';
240
241        // the -1 below directs the token iterator to display the parts of
242        // the string that did NOT match the regular expression.
243        sregex_token_iterator cur( str.begin(), str.end(), html, -1 );
244        sregex_token_iterator end;
245
246        for( ; cur != end; ++cur )
247        {
248            std::cout << '{' << *cur << '}';
249        }
250        std::cout << '\n';
251
252        return 0;
253    }
254
255This program outputs the following:
256
257[pre
258{Now }{is the time }{for all good men}{ to come to the aid of their}{ country.}
259]
260\n
261[link boost_xpressive.user_s_guide.examples top]
262
263----
264
265[h4 Display a tree of nested results]
266
267Here is a helper class to demonstrate how you might display a tree of nested results:
268
269    // Displays nested results to std::cout with indenting
270    struct output_nested_results
271    {
272        int tabs_;
273
274        output_nested_results( int tabs = 0 )
275            : tabs_( tabs )
276        {
277        }
278
279        template< typename BidiIterT >
280        void operator ()( match_results< BidiIterT > const &what ) const
281        {
282            // first, do some indenting
283            typedef typename std::iterator_traits< BidiIterT >::value_type char_type;
284            char_type space_ch = char_type(' ');
285            std::fill_n( std::ostream_iterator<char_type>( std::cout ), tabs_ * 4, space_ch );
286
287            // output the match
288            std::cout << what[0] << '\n';
289
290            // output any nested matches
291            std::for_each(
292                what.nested_results().begin(),
293                what.nested_results().end(),
294                output_nested_results( tabs_ + 1 ) );
295        }
296    };
297
298[link boost_xpressive.user_s_guide.examples top]
299
300[endsect]
Note: See TracBrowser for help on using the repository browser.