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

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

Wiki がページ「正規表現で文字列の検索(C++)」を「テクニック - 正規表現で文字列の検索(C++)」に、リダイレクトを残さずに移動しました
文字列「<source lang="c++">」を「<syntaxhighlight lang="c++">」に置換
1行目: 1行目:
== 概要 ==
== 概要 ==
regex.hのstd::regex_search関数 / std::regex_match関数を用いることで、正規表現による部分一致または完全一致を判定することができる。<br>
regex.hのstd::regex_search関数 / std::regex_match関数を用いることで、正規表現による部分一致または完全一致を判定することができる。<br>
  <source lang="c++">
  <syntaxhighlight lang="c++">
  #include <regex>
  #include <regex>
   
   
21行目: 21行目:
std::regex_search関数は、検索先の文字列に正規表現パターンが含まれているかどうかを判定する。<br>
std::regex_search関数は、検索先の文字列に正規表現パターンが含まれているかどうかを判定する。<br>
JavaScriptにおける部分マッチの処理/./.test("ab")に相当する。<br>
JavaScriptにおける部分マッチの処理/./.test("ab")に相当する。<br>
  <source lang="c++">
  <syntaxhighlight lang="c++">
  #include <regex>
  #include <regex>
   
   
35行目: 35行目:
std::regex_match関数は、検索先の文字列全体が正規表現パターンに一致するかどうかを判定する。<br>
std::regex_match関数は、検索先の文字列全体が正規表現パターンに一致するかどうかを判定する。<br>
完全なパターンマッチを判定する用途に利用できる。JavaScriptにおける全文一致の処理/^..$/.test("ab")に相当する。<br>
完全なパターンマッチを判定する用途に利用できる。JavaScriptにおける全文一致の処理/^..$/.test("ab")に相当する。<br>
  <source lang="c++">
  <syntaxhighlight lang="c++">
  #include <regex>
  #include <regex>
   
   
48行目: 48行目:
== 大文字 / 小文字の区別(std::regex::icaseフラグ) ==
== 大文字 / 小文字の区別(std::regex::icaseフラグ) ==
大文字と小文字の区別を行わない場合には、std::regexの第1引数にstd::regex::icaseフラグを指定する。<br>
大文字と小文字の区別を行わない場合には、std::regexの第1引数にstd::regex::icaseフラグを指定する。<br>
  <source lang="c++">
  <syntaxhighlight lang="c++">
  std::regex_match("ABC", std::regex("abc", std::regex::icase));    // true
  std::regex_match("ABC", std::regex("abc", std::regex::icase));    // true
  std::regex_search("ABC", std::regex("a", std::regex::icase));      // true
  std::regex_search("ABC", std::regex("a", std::regex::icase));      // true
56行目: 56行目:
std::regex::icaseフラグは、std::regex_constants::syntax_option_type列挙体型の定数値として定義されている。<br>
std::regex::icaseフラグは、std::regex_constants::syntax_option_type列挙体型の定数値として定義されている。<br>
同等の値は、std::regex_constants::icaseと言う形で取得することもできる。<br>
同等の値は、std::regex_constants::icaseと言う形で取得することもできる。<br>
  <source lang="c++">
  <syntaxhighlight lang="c++">
  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));
65行目: 65行目:
R"()"で生文字リテラルによるヒアドキュメントを表現することもできる。<br>
R"()"で生文字リテラルによるヒアドキュメントを表現することもできる。<br>
これにより、\(バックスラッシュ)や"(ダブルクォーテーション)をエスケープ記号無しに記述することができる。<br>
これにより、\(バックスラッシュ)や"(ダブルクォーテーション)をエスケープ記号無しに記述することができる。<br>
  <source 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>
  </source>
<br>
<br>
イテレータを指定することで、コンテナクラスへの検索もできる。<br>
イテレータを指定することで、コンテナクラスへの検索もできる。<br>
  <source lang="c++">
  <syntaxhighlight lang="c++">
  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
76行目: 76行目:
<br>
<br>
イテレータで特定の範囲を指定することで、部分文字列の検索もできる。<br>
イテレータで特定の範囲を指定することで、部分文字列の検索もできる。<br>
  <source lang="c++">
  <syntaxhighlight lang="c++">
  // "tring"の範囲で検索
  // "tring"の範囲で検索
  std::string s = "CString";
  std::string s = "CString";
87行目: 87行目:
<br>
<br>
std::basic_regex<T>という形で特定の型の文字列に対応することもできる。<br>
std::basic_regex<T>という形で特定の型の文字列に対応することもできる。<br>
  <source lang="c++">
  <syntaxhighlight lang="c++">
  std::regex_match(std::basic_string<char>("a"), std::basic_regex<char>(".")); // true
  std::regex_match(std::basic_string<char>("a"), std::basic_regex<char>(".")); // true