2 / Copyright (c) 2008 Eric Niebler
4 / Distributed under the Boost Software License, Version 1.0. (See accompanying
5 / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 Below you can find six complete sample programs.
15 [h4 See if a whole string matches a regex]
17 This is the example from the Introduction. It is reproduced here for your convenience.
20 #include <boost/xpressive/xpressive.hpp>
22 using namespace boost::xpressive;
26 std::string hello( "hello world!" );
28 sregex rex = sregex::compile( "(\\w+) (\\w+)!" );
31 if( regex_match( hello, what, rex ) )
33 std::cout << what[0] << '\n'; // whole match
34 std::cout << what[1] << '\n'; // first capture
35 std::cout << what[2] << '\n'; // second capture
41 This program outputs the following:
49 [link boost_xpressive.user_s_guide.examples top]
53 [h4 See if a string contains a sub-string that matches a regex]
55 Notice in this example how we use custom `mark_tag`s to make the pattern more readable.
56 We can use the `mark_tag`s later to index into the _match_results_.
59 #include <boost/xpressive/xpressive.hpp>
61 using namespace boost::xpressive;
65 char const *str = "I was born on 5/30/1973 at 7am.";
67 // define some custom mark_tags with names more meaningful than s1, s2, etc.
68 mark_tag day(1), month(2), year(3), delim(4);
70 // this regex finds a date
71 cregex date = (month= repeat<1,2>(_d)) // find the month ...
72 >> (delim= (set= '/','-')) // followed by a delimiter ...
73 >> (day= repeat<1,2>(_d)) >> delim // and a day followed by the same delimiter ...
74 >> (year= repeat<1,2>(_d >> _d)); // and the year.
78 if( regex_search( str, what, date ) )
80 std::cout << what[0] << '\n'; // whole match
81 std::cout << what[day] << '\n'; // the day
82 std::cout << what[month] << '\n'; // the month
83 std::cout << what[year] << '\n'; // the year
84 std::cout << what[delim] << '\n'; // the delimiter
90 This program outputs the following:
100 [link boost_xpressive.user_s_guide.examples top]
104 [h4 Replace all sub-strings that match a regex]
106 The following program finds dates in a string and marks them up with pseudo-HTML.
109 #include <boost/xpressive/xpressive.hpp>
111 using namespace boost::xpressive;
115 std::string str( "I was born on 5/30/1973 at 7am." );
117 // essentially the same regex as in the previous example, but using a dynamic regex
118 sregex date = sregex::compile( "(\\d{1,2})([/-])(\\d{1,2})\\2((?:\\d{2}){1,2})" );
120 // As in Perl, $& is a reference to the sub-string that matched the regex
121 std::string format( "<date>$&</date>" );
123 str = regex_replace( str, date, format );
124 std::cout << str << '\n';
129 This program outputs the following:
132 I was born on <date>5/30/1973</date> at 7am.
135 [link boost_xpressive.user_s_guide.examples top]
139 [h4 Find all the sub-strings that match a regex and step through them one at a time]
141 The following program finds the words in a wide-character string.
142 It uses `wsregex_iterator`. Notice that dereferencing a `wsregex_iterator`
143 yields a `wsmatch` object.
146 #include <boost/xpressive/xpressive.hpp>
148 using namespace boost::xpressive;
152 std::wstring str( L"This is his face." );
155 wsregex token = +alnum;
157 wsregex_iterator cur( str.begin(), str.end(), token );
158 wsregex_iterator end;
160 for( ; cur != end; ++cur )
162 wsmatch const &what = *cur;
163 std::wcout << what[0] << L'\n';
169 This program outputs the following:
178 [link boost_xpressive.user_s_guide.examples top]
182 [h4 Split a string into tokens that each match a regex]
184 The following program finds race times in a string and displays first
185 the minutes and then the seconds. It uses _regex_token_iterator_.
188 #include <boost/xpressive/xpressive.hpp>
190 using namespace boost::xpressive;
194 std::string str( "Eric: 4:40, Karl: 3:35, Francesca: 2:32" );
197 sregex time = sregex::compile( "(\\d):(\\d\\d)" );
199 // for each match, the token iterator should first take the value of
200 // the first marked sub-expression followed by the value of the second
201 // marked sub-expression
202 int const subs[] = { 1, 2 };
204 sregex_token_iterator cur( str.begin(), str.end(), time, subs );
205 sregex_token_iterator end;
207 for( ; cur != end; ++cur )
209 std::cout << *cur << '\n';
215 This program outputs the following:
226 [link boost_xpressive.user_s_guide.examples top]
230 [h4 Split a string using a regex as a delimiter]
232 The following program takes some text that has been marked up with html and strips
233 out the mark-up. It uses a regex that matches an HTML tag and a _regex_token_iterator_
234 that returns the parts of the string that do ['not] match the regex.
237 #include <boost/xpressive/xpressive.hpp>
239 using namespace boost::xpressive;
243 std::string str( "Now <bold>is the time <i>for all good men</i> to come to the aid of their</bold> country." );
246 sregex html = '<' >> optional('/') >> +_w >> '>';
248 // the -1 below directs the token iterator to display the parts of
249 // the string that did NOT match the regular expression.
250 sregex_token_iterator cur( str.begin(), str.end(), html, -1 );
251 sregex_token_iterator end;
253 for( ; cur != end; ++cur )
255 std::cout << '{' << *cur << '}';
262 This program outputs the following:
265 {Now }{is the time }{for all good men}{ to come to the aid of their}{ country.}
268 [link boost_xpressive.user_s_guide.examples top]
272 [h4 Display a tree of nested results]
274 Here is a helper class to demonstrate how you might display a tree of nested results:
276 // Displays nested results to std::cout with indenting
277 struct output_nested_results
281 output_nested_results( int tabs = 0 )
286 template< typename BidiIterT >
287 void operator ()( match_results< BidiIterT > const &what ) const
289 // first, do some indenting
290 typedef typename std::iterator_traits< BidiIterT >::value_type char_type;
291 char_type space_ch = char_type(' ');
292 std::fill_n( std::ostream_iterator<char_type>( std::cout ), tabs_ * 4, space_ch );
295 std::cout << what[0] << '\n';
297 // output any nested matches
299 what.nested_results().begin(),
300 what.nested_results().end(),
301 output_nested_results( tabs_ + 1 ) );
305 [link boost_xpressive.user_s_guide.examples top]