「テクニック - 正規表現で文字列の検索(C++)」の版間の差分

📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

文字列「<source lang="c++">」を「<syntaxhighlight lang="c++">」に置換
文字列「</source>」を「</syntaxhighlight>」に置換
 
9行目: 9行目:
  // 完全一致
  // 完全一致
  std::regex_match("u32", std::regex("(i|u)\\d+"));  // true
  std::regex_match("u32", std::regex("(i|u)\\d+"));  // true
  </source>
  </syntaxhighlight>
<br>
<br>
第1引数に検索対象の文字列を指定し、第2引数に判定する正規表現のパターンをstd::regex("文字列")という形で指定する。<br>
第1引数に検索対象の文字列を指定し、第2引数に判定する正規表現のパターンをstd::regex("文字列")という形で指定する。<br>
29行目: 29行目:
  std::regex_search("9", std::regex("\\d\\d"));  // false
  std::regex_search("9", std::regex("\\d\\d"));  // false
  std::regex_search("99", std::regex("\\d\\d"));  // true
  std::regex_search("99", std::regex("\\d\\d"));  // true
  </source>
  </syntaxhighlight>
<br><br>
<br><br>


43行目: 43行目:
   
   
  std::regex_match("Shop 99", std::regex("\\w+ \\d+")); // true
  std::regex_match("Shop 99", std::regex("\\w+ \\d+")); // true
  </source>
  </syntaxhighlight>
<br><br>
<br><br>


52行目: 52行目:
  std::regex_search("ABC", std::regex("a", std::regex::icase));      // true
  std::regex_search("ABC", std::regex("a", std::regex::icase));      // true
  std::regex_search("IBM", std::regex("[a-c]", std::regex::icase));  // true
  std::regex_search("IBM", std::regex("[a-c]", std::regex::icase));  // true
  </source>
  </syntaxhighlight>
<br>
<br>
std::regex::icaseフラグは、std::regex_constants::syntax_option_type列挙体型の定数値として定義されている。<br>
std::regex::icaseフラグは、std::regex_constants::syntax_option_type列挙体型の定数値として定義されている。<br>
59行目: 59行目:
  std::regex_match("a", std::regex("A", std::regex_constants::icase));
  std::regex_match("a", std::regex("A", std::regex_constants::icase));
  std::regex_match(L"a", std::wregex(L"A", std::wregex::icase));
  std::regex_match(L"a", std::wregex(L"A", std::wregex::icase));
  </source>
  </syntaxhighlight>
<br><br>
<br><br>


67行目: 67行目:
  <syntaxhighlight lang="c++">
  <syntaxhighlight lang="c++">
  std::regex_match("u32", std::regex(R"(\w\d+)"));  // true
  std::regex_match("u32", std::regex(R"(\w\d+)"));  // true
  </source>
  </syntaxhighlight>
<br>
<br>
イテレータを指定することで、コンテナクラスへの検索もできる。<br>
イテレータを指定することで、コンテナクラスへの検索もできる。<br>
73行目: 73行目:
  std::vector<char> a = {'a', 'b', 'c'};
  std::vector<char> a = {'a', 'b', 'c'};
  std::regex_match(a.begin(), a.end(), std::regex(".+")); // true
  std::regex_match(a.begin(), a.end(), std::regex(".+")); // true
  </source>
  </syntaxhighlight>
<br>
<br>
イテレータで特定の範囲を指定することで、部分文字列の検索もできる。<br>
イテレータで特定の範囲を指定することで、部分文字列の検索もできる。<br>
84行目: 84行目:
  const char* s = "CString";
  const char* s = "CString";
  std::regex_match(s, s + 2, std::regex("[A-Z]+")); // true
  std::regex_match(s, s + 2, std::regex("[A-Z]+")); // true
  </source>
  </syntaxhighlight>
<br>
<br>
std::basic_regex<T>という形で特定の型の文字列に対応することもできる。<br>
std::basic_regex<T>という形で特定の型の文字列に対応することもできる。<br>
93行目: 93行目:
  // error: implicit instantiation of undefined template 'std::__1::ctype<unsigned char>'
  // error: implicit instantiation of undefined template 'std::__1::ctype<unsigned char>'
  std::basic_regex<unsigned char>(); // `std::ctype<T>`の特殊化に依存
  std::basic_regex<unsigned char>(); // `std::ctype<T>`の特殊化に依存
  </source>
  </syntaxhighlight>
<br><br>
<br><br>


__FORCETOC__
__FORCETOC__
[[カテゴリ:C++]]
[[カテゴリ:C++]]