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

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

Wiki がページ「正規表現で文字列の検索(C++)」を「テクニック - 正規表現で文字列の検索(C++)」に、リダイレクトを残さずに移動しました
文字列「</source>」を「</syntaxhighlight>」に置換
 
(同じ利用者による、間の1版が非表示)
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>
   
   
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>
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>
   
   
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>


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>
   
   
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>


== 大文字 / 小文字の区別(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
  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>
同等の値は、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));
  </source>
  </syntaxhighlight>
<br><br>
<br><br>


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>
  </syntaxhighlight>
<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
  </source>
  </syntaxhighlight>
<br>
<br>
イテレータで特定の範囲を指定することで、部分文字列の検索もできる。<br>
イテレータで特定の範囲を指定することで、部分文字列の検索もできる。<br>
  <source lang="c++">
  <syntaxhighlight lang="c++">
  // "tring"の範囲で検索
  // "tring"の範囲で検索
  std::string s = "CString";
  std::string s = "CString";
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>
  <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
   
   
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++]]