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

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

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

概要

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

<syntaxhighlight lang="c++">
#include <regex>

// 部分一致
std::regex_search("u32", std::regex("[0-9]"));     // true

// 完全一致
std::regex_match("u32", std::regex("(i|u)\\d+"));  // true
</source>


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

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


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

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

<syntaxhighlight lang="c++">
#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
</source>



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

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

<syntaxhighlight lang="c++">
#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
</source>



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

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

<syntaxhighlight lang="c++">
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
</source>


std::regex::icaseフラグは、std::regex_constants::syntax_option_type列挙体型の定数値として定義されている。
同等の値は、std::regex_constants::icaseと言う形で取得することもできる。

<syntaxhighlight lang="c++">
std::regex_match("a", std::regex("A", std::regex_constants::icase));
std::regex_match(L"a", std::wregex(L"A", std::wregex::icase));
</source>



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

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

<syntaxhighlight lang="c++">
std::regex_match("u32", std::regex(R"(\w\d+)"));  // true
</source>


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

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


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

<syntaxhighlight lang="c++">
// "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
</source>


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

<syntaxhighlight lang="c++">
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>`の特殊化に依存
</source>