最終更新日時(UTC):
が更新

履歴 編集

function
<regex>

std::basic_regex::コンストラクタ(C++11)

basic_regex();                                                              // (1)

basic_regex(const basic_regex& that);                                       // (2)

basic_regex(basic_regex&& that) noexcept;                                   // (3)

explicit basic_regex(const charT* ptr,
                     flag_type f = regex_constants::ECMAScript);            // (4)

basic_regex(const charT* ptr, size_t len, flag_type f);                     // (5)

template <class ST, class SA>
basic_regex(const basic_string<charT, ST, SA>& p,
            flag_type f = regex_constants::ECMAScript);                     // (6)

template <class ForwardIterator>
basic_regex(ForwardIterator first, ForwardIterator last,
            flag_type f = regex_constants::ECMAScript);                     // (7)

basic_regex(initializer_list<charT> il,
            flag_type f = regex_constants::ECMAScript);                     // (8)

概要

正規表現オブジェクトを構築する。

要件

  • ptr はヌルポインタではないこと。
  • InputIterator は入力イテレータの要件を満たすこと。

効果

  • (1) デフォルトコンストラクタ。いかなる文字列にもマッチしない basic_regex オブジェクトを構築する。
  • (2) コピーコンストラクタ。that をコピーして basic_regex オブジェクトを構築する。
  • (3) ムーブコンストラクタ。that をムーブして basic_regex オブジェクトを構築する。
  • (4) 引数 f で指定されたフラグに従って、先頭が ptr で長さ char_traits::length(ptr) の文字列から basic_regex オブジェクトを構築する。
    指定された文字列が有効な正規表現で無い場合には、例外 regex_error を投げる。
  • (5) 引数 f で指定されたフラグに従って、先頭が ptr で長さ len の文字列から basic_regex オブジェクトを構築する。
    指定された文字列が有効な正規表現で無い場合には、例外 regex_error を投げる。
  • (6) 引数 f で指定されたフラグに従って、p で指定された文字列から basic_regex オブジェクトを構築する。
    文字列 p が有効な正規表現で無い場合には、例外 regex_error を投げる。
  • (7) 引数 f で指定されたフラグに従って、範囲 [first, last) で指定された文字列から basic_regex オブジェクトを構築する。
    文字列 [first, last) が有効な正規表現で無い場合には、例外 regex_error を投げる。
  • (8) basic_regex(il.begin(), il.end(), f) と等価。

事後条件

  • (1) -
  • (2) flags()mark_count() は、それぞれ that.flags()that.mark_count を返す。
  • (3) flags()mark_count() は、それぞれ that.flags()that.mark_count の元の値を返す。
    that未規定の有効な状態である。
  • (4) flags() は引数 f を、mark_count() は指定された正規表現内のキャプチャグループの数を返す。
  • (5) flags() は引数 f を、mark_count() は指定された正規表現内のキャプチャグループの数を返す。
  • (6) flags() は引数 f を、mark_count() は指定された正規表現内のキャプチャグループの数を返す。
  • (7) flags() は引数 f を、mark_count() は指定された正規表現内のキャプチャグループの数を返す。
  • (8) -

備考

#include <iostream>
#include <regex>
#include <string>

int main()
{
  const char s[] = " abc ";
  std::cout << std::boolalpha;

  const std::regex re1;                                                 // (1) の形式
  std::cout << std::regex_search(s, re1) << std::endl;

  std::regex re4("\\w+");                                               // (4) の形式
  std::cout << std::regex_search(s, re4) << std::endl;

  const std::regex re2(re4);                                            // (2) の形式
  std::cout << std::regex_search(s, re2) << std::endl;

  const std::regex re3(std::move(re4));                                 // (3) の形式
  std::cout << std::regex_search(s, re3) << std::endl;

  const std::regex re5("ABC\\d+", 3, std::regex_constants::icase);      // (5) の形式
  std::cout << std::regex_search(s, re5) << std::endl;

  const std::string restr("ABC");
  const std::regex re6(restr, std::regex_constants::icase);             // (6) の形式
  std::cout << std::regex_search(s, re6) << std::endl;

  const std::string p("[[:alpha:]]+");
  const std::regex re7(std::begin(p), std::end(p));                     // (7) の形式
  std::cout << std::regex_search(s, re7) << std::endl;

  const std::regex re8{ '\\', 'd', '+' };                               // (8) の形式
  std::cout << std::regex_search(s, re8) << std::endl;
}

出力

false
true
true
true
true
true
true
false

バージョン

言語

  • C++11

処理系

備考

Clang バージョン 3.0 は initializer_list に対応していないため、(8) の形式は提供されていない。

参照