fork download
  1. #include <iostream>
  2. #include <iterator>
  3. #include <string>
  4. #include <regex>
  5.  
  6. int main()
  7. {
  8. std::string s = "Some people, when confronted with a problem, think "
  9. "\"I know, I'll use regular expressions.\" "
  10. "Now they have two problems.";
  11.  
  12. std::regex self_regex("REGULAR EXPRESSIONS",
  13. std::regex_constants::ECMAScript | std::regex_constants::icase);
  14. if (std::regex_search(s, self_regex)) {
  15. std::cout << "Text contains the phrase 'regular expressions'\n";
  16. }
  17.  
  18. std::regex word_regex("(\\S+)");
  19. auto words_begin =
  20. std::sregex_iterator(s.begin(), s.end(), word_regex);
  21. auto words_end = std::sregex_iterator();
  22.  
  23. std::cout << "Found "
  24. << std::distance(words_begin, words_end)
  25. << " words\n";
  26.  
  27. const int N = 6;
  28. std::cout << "Words greater than " << N << " characters:\n";
  29. for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
  30. std::smatch match = *i;
  31. std::string match_str = match.str();
  32. if (match_str.size() > N) {
  33. std::cout << " " << match_str << '\n';
  34. }
  35. }
  36.  
  37. std::regex long_word_regex("(\\w{7,})");
  38. std::string new_s = std::regex_replace(s, long_word_regex, "[$&]");
  39. std::cout << new_s << '\n';
  40. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:32:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
prog.cpp:38:70: error: no matching function for call to ‘regex_replace(std::string&, std::regex&, const char [5])’
prog.cpp:38:70: note: candidates are:
In file included from /usr/include/c++/4.7/regex:62:0,
                 from prog.cpp:4:
/usr/include/c++/4.7/bits/regex.h:2200:5: note: template<class _Out_iter, class _Bi_iter, class _Rx_traits, class _Ch_type> _Out_iter std::regex_replace(_Out_iter, _Bi_iter, _Bi_iter, const std::basic_regex<_Ch_type, _Rx_traits>&, const std::basic_string<_Ch_type>&, std::regex_constants::match_flag_type)
/usr/include/c++/4.7/bits/regex.h:2200:5: note:   template argument deduction/substitution failed:
prog.cpp:38:70: note:   deduced conflicting types for parameter ‘_Bi_iter’ (‘std::basic_regex<char>’ and ‘const char*’)
In file included from /usr/include/c++/4.7/regex:62:0,
                 from prog.cpp:4:
/usr/include/c++/4.7/bits/regex.h:2220:5: note: template<class _Rx_traits, class _Ch_type> std::basic_string<_Ch_type> std::regex_replace(const std::basic_string<_Ch_type>&, const std::basic_regex<_Ch_type, _Rx_traits>&, const std::basic_string<_Ch_type>&, std::regex_constants::match_flag_type)
/usr/include/c++/4.7/bits/regex.h:2220:5: note:   template argument deduction/substitution failed:
prog.cpp:38:70: note:   mismatched types ‘const std::basic_string<_Ch_type>’ and ‘const char [5]’
stdout
Standard output is empty