examples.qbk 7.5 KB

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