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

履歴 編集

function template
<regex>

std::regex_match(C++11)

namespace std {
  template <class BidirectionalIterator, class Allocator, class charT, class traits>
  bool regex_match(BidirectionalIterator first, BidirectionalIterator last,
                   match_results<BidirectionalIterator, Allocator>& m,
                   const basic_regex<charT, traits>& e,
                   regex_constants::match_flag_type flags = regex_constants::match_default);          // (1)

  template <class charT, class Allocator, class traits>
  bool regex_match(const charT* str,
                   match_results<const charT*, Allocator>& m,
                   const basic_regex<charT, traits>& e,
                   regex_constants::match_flag_type flags = regex_constants::match_default);          // (2)

  template <class ST, class SA, class Allocator, class charT, class traits>
  bool regex_match(const basic_string<charT, ST, SA>& s,
                   match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
                   const basic_regex<charT, traits>& e,
                   regex_constants::match_flag_type flags = regex_constants::match_default);          // (3)

  template <class ST, class SA, class Allocator, class charT, class traits>
  bool regex_match(basic_string<charT, ST, SA>&& s,
                   match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
                   const basic_regex<charT, traits>& e,
                   regex_constants::match_flag_type flags = regex_constants::match_default) = delete; // (4) C++14 から

  template <class BidirectionalIterator, class charT, class traits>
  bool regex_match(BidirectionalIterator first, BidirectionalIterator last,
                   const basic_regex<charT, traits>& e,
                   regex_constants::match_flag_type flags = regex_constants::match_default);          // (5)

  template <class charT, class traits>
  bool regex_match(const charT* str,
                   const basic_regex<charT, traits>& e,
                   regex_constants::match_flag_type flags = regex_constants::match_default);          // (6)

  template <class ST, class SA, class charT, class traits>
  bool regex_match(const basic_string<charT, ST, SA>& s,
                   const basic_regex<charT, traits>& e,
                   regex_constants::match_flag_type flags = regex_constants::match_default);          // (7)
}

概要

指定された文字列全体が、正規表現にマッチするか否かの判定を行う。
引数に match_results がある場合、当該オブジェクトにマッチの結果を格納する。
なお、同様の関数である regex_search と異なり、正規表現が文字列全体にマッチしなければならない。

要件

BidirectionalIterator は双方向イテレータの要件を満たすこと。

効果

  • (1) [first, last) で指定された文字列全体が、e で指定された正規表現にマッチするか否かの判定を行う。
    flags は正規表現が文字列に対してどのようにマッチするかを指定する。
  • (2) return regex_match(str, str + char_traits::length(str), m, e, flags) と等価。
  • (3) return regex_match(s.begin(), s.end(), m, e, flags) と等価。
  • (4) deleted 宣言されているため、使用するとコンパイルエラーとなる。
  • (5) match_results<BidirectionalIterator> 型のダミーオブジェクト what を構築し、return regex_match(first, last, what, e, flags) としたものと等価。
  • (6) return regex_match(str, str + char_traits::length(str), e, flags) と等価。
  • (7) return regex_match(s.begin(), s.end(), e, flags) と等価。

事後条件

  • (1) 常に m.ready() == true となる。
    もし、戻り値が false の場合、mready() 以外の状態については、m.size() == 0 および m.empty() == true となる事以外は未規定である。
    もし、戻り値が true の場合、mready() 以外の状態は以下の表を満たす。

    要素
    m.size() 1 + e.mark_count()
    m.empty() false
    m.prefix().first first
    m.prefix().second first
    m.prefix().matched false
    m.suffix().first last
    m.suffix().second last
    m.suffix().matched false
    m.[0].first first
    m.[0].second last
    m.[0].matched true
    m.[n].first 0 < n < m.size() となるすべての整数 n について、正規表現内の n 番目のキャプチャグループがマッチした文字列の最初の文字を指すイテレータ。
    もし、n 番目のキャプチャグループがマッチに参加していない場合には、last
    m.[n].second 0 < n < m.size() となるすべての整数 n について、正規表現内の n 番目のキャプチャグループがマッチした文字列の最後の文字の次を指すイテレータ。
    もし、n 番目のキャプチャグループがマッチに参加していない場合には、last
    m.[n].matched 0 < n < m.size() となるすべての整数 n について、正規表現内の n 番目のキャプチャグループがマッチに参加していれば true
    もし、n 番目のキャプチャグループがマッチに参加していない場合には、false

戻り値

指定した文字列全体が、正規表現にマッチした場合、true。マッチしなかった場合は false

例外

本関数は regex_error を送出する可能性がある。
もしそのような例外 e が送出された場合、 e.code()regex_constants::error_complexityregex_constants::error_stack のいずれかである。

備考

match_results オブジェクトを引数に取る形式の場合、そのオブジェクトは引数で指定した検索対象文字列へのイテレータを保持する。
このため、検索対象文字列は本関数を呼び出した後も match_results オブジェクトを使用し終わるまで破棄されないようにする必要がある。
従って、(3) の形式に渡す引数 s に一時オブジェクトを指定することはほぼ間違いなくプログラミング上のエラーを意味する。
(4) の形式が deleted として C++14 で追加された理由は、このような事態をコンパイル時に検出するためである。

#include <iostream>
#include <iterator>
#include <list>
#include <regex>
#include <string>

int main()
{
  std::cout << std::boolalpha;
  {
    // (1) の形式
    const std::list<char> s = { 'a', 'b', 'c', '1', '2', '3', 'd', 'e', 'f' };
    std::match_results<std::list<char>::const_iterator> m;
    std::cout << "(1) " << std::regex_match(std::begin(s), std::end(s), m, std::regex("\\w+")) << std::endl;
    std::cout << "str = '" << m.str() << "', position = " << m.position() << std::endl;
  }
  {
    // (2) の形式
    std::cmatch m;
    std::cout << "(2) " << std::regex_match("abc123def", m, std::regex("\\w+")) << std::endl;
    std::cout << "str = '" << m.str() << "', position = " << m.position() << std::endl;
  }
  {
    // (3) の形式
    const std::string s = "abc123def";
    std::smatch m;
    std::cout << "(3) " << std::regex_match(s, m, std::regex("\\w+")) << std::endl;
    std::cout << "str = '" << m.str() << "', position = " << m.position() << std::endl;
  }
  {
    // (4) の形式(コメントアウトを外すと C++14 ではエラーになる)
    //std::smatch m;
    //std::cout << "(4) " << std::regex_match(std::string("abc123def"), m, std::regex("\\w+")) << std::endl;
    //std::cout << "str = '" << m.str() << "', position = " << m.position() << std::endl;
  }
  {
    // (5) の形式
    const std::list<char> s = { 'a', 'b', 'c', '1', '2', '3', 'd', 'e', 'f' };
    std::cout << "(5) " << std::regex_match(std::begin(s), std::end(s), std::regex("\\w+")) << std::endl;
  }
  {
    // (6) の形式
    std::cout << "(6) " << std::regex_match("abc123def", std::regex("\\w+")) << std::endl;
  }
  {
    // (7) の形式、その1
    const std::string s = "abc123def";
    std::cout << "(7)-1 " << std::regex_match(s, std::regex("\\w+")) << std::endl;
  }
  {
    // (7) の形式、その2(C++14 でもエラーにならない)
    std::cout << "(7)-2 " << std::regex_match(std::string("abc123def"), std::regex("\\w+")) << std::endl;
  }
}

出力

(1) = true
str = 'abc123def', position = 0
(2) true
str = 'abc123def', position = 0
(3) true
str = 'abc123def', position = 0
(5) true
(6) true
(7)-1 true
(7)-2 true

バージョン

言語

  • C++11

処理系

備考

Clang(libc++) では、3.4 までは (4) の形式は存在しない。
GCC(libstdc++) では、4.9.2 までは (4) の形式は存在しない。

参照