- regex[meta header]
- std[meta namespace]
- function template[meta id-type]
- cpp11[meta cpp]
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[link match_results.md]
- basic_regex[link basic_regex.md]
- regex_constants::match_default[link regex_constants/match_flag_type.md]
- basic_string[link ../string/basic_string.md]
指定された文字列全体が、正規表現にマッチするか否かの判定を行う。
引数に 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
の場合、m
のready
()
以外の状態については、m.
size
() == 0
およびm.
empty
() == true
となる事以外は未規定である。
もし、戻り値がtrue
の場合、m
のready
()
以外の状態は以下の表を満たす。要素 値 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_complexity
か regex_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;
}
}
- std::regex_match[color ff0000]
- std::regex[link basic_regex.md]
- std::match_results[link match_results.md]
- std::cmatch[link match_results.md]
- std::smatch[link match_results.md]
- m.str()[link match_results/str.md]
- m.position()[link match_results/position.md]
(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: 3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6
- GCC: 4.9.0, 4.9.1, 4.9.2, 5.0.0
- ICC: ??
- Visual C++: ??
Clang(libc++) では、3.4 までは (4) の形式は存在しない。
GCC(libstdc++) では、4.9.2 までは (4) の形式は存在しない。