Advertisement
Naohiro19

C++で3文字以上の単語を表示するプログラム(正規表現使用)

Jan 21st, 2022
958
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <regex>
  4. #include <vector>
  5.  
  6. std::vector<std::string>
  7. search(std::string const& text, std::regex const& re)
  8. {
  9.     std::vector<std::string> result;
  10.    
  11.     auto iter = text.cbegin();
  12.     auto end = text.cend();
  13.  
  14.     std::smatch m;
  15.     while (std::regex_search(iter, end, m, re)) {
  16.         result.push_back(m.str());
  17.         iter = m[0].second;
  18.     }
  19.     return result;
  20. }
  21.  
  22. int main()
  23. {
  24.     std::regex re(R"(\w+)");
  25.     std::string text = "Yes, we can.";
  26.  
  27.     auto result = search(text, re);
  28.     for (auto e : result) {
  29.         if(e.length() >= 3) // 文字列の長さが3文字以上
  30.         std::cout << e << std::endl;
  31.     }
  32.  
  33. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement