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