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

提供:MochiuWiki : SUSE, EC, PCB
ナビゲーションに移動 検索に移動
(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++]]

2021年11月24日 (水) 18:06時点における最新版

概要

regex.hのstd::regex_search関数 / std::regex_match関数を用いることで、正規表現による部分一致または完全一致を判定することができる。

 #include <regex>
 
 // 部分一致
 std::regex_search("u32", std::regex("[0-9]"));     // true
 
 // 完全一致
 std::regex_match("u32", std::regex("(i|u)\\d+"));  // true


第1引数に検索対象の文字列を指定し、第2引数に判定する正規表現のパターンをstd::regex("文字列")という形で指定する。
判定結果はbool型の戻り値として返される。(マッチした場合 : true、マッチしなかった場合 : false)

一致した文字列の抽出や出現位置の判定を行いたい場合には、以下のページを参考にする。
正規表現による検索文字列の抽出/出現位置の判定|std::match_results


部分一致(std::regex_search関数)

std::regex_search関数は、検索先の文字列に正規表現パターンが含まれているかどうかを判定する。
JavaScriptにおける部分マッチの処理/./.test("ab")に相当する。

 #include <regex>
 
 std::regex_search("abc", std::regex("\\w"));               // true
 std::regex_search(std::string("123"), std::regex("\\d"));  // true
 
 std::regex_search("9", std::regex("\\d\\d"));   // false
 std::regex_search("99", std::regex("\\d\\d"));  // true



完全一致(std::regex_match関数)

std::regex_match関数は、検索先の文字列全体が正規表現パターンに一致するかどうかを判定する。
完全なパターンマッチを判定する用途に利用できる。JavaScriptにおける全文一致の処理/^..$/.test("ab")に相当する。

 #include <regex>
 
 std::regex_match("abc", std::regex("\\w+")); // true
 std::regex_match("abc", std::regex("."));    // false
 std::regex_match("abc", std::regex(".+"));   // true
 
 std::regex_match("Shop 99", std::regex("\\w+ \\d+")); // true



大文字 / 小文字の区別(std::regex::icaseフラグ)

大文字と小文字の区別を行わない場合には、std::regexの第1引数にstd::regex::icaseフラグを指定する。

 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("IBM", std::regex("[a-c]", std::regex::icase));  // true


std::regex::icaseフラグは、std::regex_constants::syntax_option_type列挙体型の定数値として定義されている。
同等の値は、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));



正規表現機能の応用(ヒアドキュメント、イテレータ、部分検索)

R"()"で生文字リテラルによるヒアドキュメントを表現することもできる。
これにより、\(バックスラッシュ)や"(ダブルクォーテーション)をエスケープ記号無しに記述することができる。

 std::regex_match("u32", std::regex(R"(\w\d+)"));  // true


イテレータを指定することで、コンテナクラスへの検索もできる。

 std::vector<char> a = {'a', 'b', 'c'};
 std::regex_match(a.begin(), a.end(), std::regex(".+")); // true


イテレータで特定の範囲を指定することで、部分文字列の検索もできる。

 // "tring"の範囲で検索
 std::string s = "CString";
 std::regex_match(begin(s) + 2, end(s), std::regex("[a-z]+")); // true
 
 // "CS"の範囲で検索
 const char* s = "CString";
 std::regex_match(s, s + 2, std::regex("[A-Z]+")); // true


std::basic_regex<T>という形で特定の型の文字列に対応することもできる。

 std::regex_match(std::basic_string<char>("a"), std::basic_regex<char>(".")); // true
 
 // unsigned char, char16_t, char32_t など、実装によっては利用できないものもある
 // error: implicit instantiation of undefined template 'std::__1::ctype<unsigned char>'
 std::basic_regex<unsigned char>(); // `std::ctype<T>`の特殊化に依存