regex_example.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Boost string_algo library example file ---------------------------------//
  2. // Copyright Pavol Droba 2002-2003. Use, modification and
  3. // distribution is subject to the Boost Software License, Version
  4. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org for updates, documentation, and revision history.
  7. #include <string>
  8. #include <iostream>
  9. #include <iterator>
  10. #include <boost/regex.hpp>
  11. #include <boost/algorithm/string/regex.hpp>
  12. using namespace std;
  13. using namespace boost;
  14. int main()
  15. {
  16. cout << "* Regex Example *" << endl << endl;
  17. string str1("abc__(456)__123__(123)__cde");
  18. // Replace all substrings matching (digit+)
  19. cout <<
  20. "replace all (digit+) in str1 with #digit+# :" <<
  21. replace_all_regex_copy( str1, regex("\\(([0-9]+)\\)"), string("#$1#") ) << endl;
  22. // Erase all substrings matching (digit+)
  23. cout <<
  24. "remove all sequences of letters from str1 :" <<
  25. erase_all_regex_copy( str1, regex("[[:alpha:]]+") ) << endl;
  26. // in-place regex transformation
  27. replace_all_regex( str1, regex("_(\\([^\\)]*\\))_"), string("-$1-") );
  28. cout << "transformad str1: " << str1 << endl;
  29. cout << endl;
  30. return 0;
  31. }