basic_regex クラス
正規表現をラップします。
構文
template <class Elem, class RXtraits>
class basic_regex
パラメーター
Elem
一致させる要素の型。
RXtraits
要素の特徴 (traits) クラス。
解説
このクラス テンプレートは、正規表現を保持するオブジェクトを表します。 このクラス テンプレートのオブジェクトは、 regex_match、 regex_search、および regex_replaceテンプレート関数に渡すことができます。 また、正規表現に一致するテキストを検索するために、適切なテキスト文字列引数を渡します。 このクラス テンプレートには特定の型定義に対する 2 つの特殊化があります。char
型の要素に特殊化した regex と、wchar_t
型の要素に特殊化した wregex です。
テンプレートの引数 RXtraits は、このクラス テンプレートでサポートされている正規表現の構文に関連した各種の重要なプロパティを表します。 こうした正規表現の特性を指定するクラスは、regex_traits クラス 型のオブジェクトと同じ外部インターフェイスを持っている必要があります。
一部の関数は、正規表現を定義するオペランド シーケンスを受け取ります。 その場合、オペランド シーケンスは次のような方法で指定できます。
ptr
: 終端要素がvalue_type()
値であり、オペランド シーケンスの一部ではないptr
で始まる null で終わるシーケンス (Elem の場合は C 文字列などchar
の)
ptr
、count
: ptr
で始まるcount
要素のシーケンス (null ポインターにすることはできません)
str
: basic_string
オブジェクトで指定されたシーケンス str
first
、 last
: 範囲内の反復子 first
および last
で区切られた要素のシーケンス [first, last)
right
: basic_regex
オブジェクト right
上記のメンバー関数は、RXtraits 型で説明されているオプションに加えて、正規表現の解釈に関するさまざまなオプションを指定する引数flags
も受け取ります。
メンバー
メンバー | Default Value |
---|---|
public static const flag_type icase | regex_constants::icase |
public static const flag_type nosubs | regex_constants::nosubs |
public static const flag_type optimize | regex_constants::optimize |
public static const flag_type collate | regex_constants::collate |
public static const flag_type ECMAScript | regex_constants::ECMAScript |
public static const flag_type basic | regex_constants::basic |
public static const flag_type extended | regex_constants::extended |
public static const flag_type awk | regex_constants::awk |
public static const flag_type grep | regex_constants::grep |
public static const flag_type egrep | regex_constants::egrep |
private RXtraits の特徴 |
コンストラクター
コンストラクター | 説明 |
---|---|
basic_regex | 正規表現オブジェクトを構築します。 |
Typedefs
型名 | 説明 |
---|---|
flag_type | 構文オプション フラグの型です。 |
locale_type | 格納されているロケール オブジェクトの型。 |
value_type | 要素タイプ。 |
メンバー関数
メンバー関数 | 説明 |
---|---|
assign | 値を正規表現オブジェクトに代入します。 |
flags | 構文のオプション フラグを返します。 |
getloc | 格納されているロケール オブジェクトを返します。 |
imbue | 格納されているロケール オブジェクトを変更します。 |
mark_count | 一致した部分式の数を返します。 |
スワップ | 2 つの正規表現オブジェクトを交換します。 |
演算子
演算子 | 説明 |
---|---|
operator= | 値を正規表現オブジェクトに代入します。 |
要件
ヘッダー: <regex>
名前空間: std
例
// std__regex__basic_regex.cpp
// compile with: /EHsc
#include <regex>
#include <iostream>
using namespace std;
int main()
{
regex::value_type elem = 'x';
regex::flag_type flag = regex::grep;
elem = elem; // to quiet "unused" warnings
flag = flag;
// constructors
regex rx0;
cout << "match(\"abc\", \"\") == " << boolalpha
<< regex_match("abc", rx0) << endl;
regex rx1("abcd", regex::ECMAScript);
cout << "match(\"abc\", \"abcd\") == " << boolalpha
<< regex_match("abc", rx1) << endl;
regex rx2("abcd", 3);
cout << "match(\"abc\", \"abc\") == " << boolalpha
<< regex_match("abc", rx2) << endl;
regex rx3(rx2);
cout << "match(\"abc\", \"abc\") == " << boolalpha
<< regex_match("abc", rx3) << endl;
string str("abcd");
regex rx4(str);
cout << "match(string(\"abcd\"), \"abc\") == " << boolalpha
<< regex_match("abc", rx4) << endl;
regex rx5(str.begin(), str.end() - 1);
cout << "match(string(\"abc\"), \"abc\") == " << boolalpha
<< regex_match("abc", rx5) << endl;
cout << endl;
// assignments
rx0 = "abc";
rx0 = rx1;
rx0 = str;
rx0.assign("abcd", regex::ECMAScript);
rx0.assign("abcd", 3);
rx0.assign(rx1);
rx0.assign(str);
rx0.assign(str.begin(), str.end() - 1);
rx0.swap(rx1);
// mark_count
cout << "\"abc\" mark_count == "
<< regex("abc").mark_count() << endl;
cout << "\"(abc)\" mark_count == "
<< regex("(abc)").mark_count() << endl;
// locales
regex::locale_type loc = rx0.imbue(locale());
cout << "getloc == imbued == " << boolalpha
<< (loc == rx0.getloc()) << endl;
// initializer_list
regex rx6({ 'a', 'b', 'c' }, regex::ECMAScript);
cout << "match(\"abc\") == " << boolalpha
<< regex_match("abc", rx6);
cout << endl;
}
match("abc", "") == false
match("abc", "abcd") == false
match("abc", "abc") == true
match("abc", "abc") == true
match(string("abcd"), "abc") == false
match(string("abc"), "abc") == true
"abc" mark_count == 0
"(abc)" mark_count == 1
getloc == imbued == true
match("abc") == true
basic_regex::assign
値を正規表現オブジェクトに代入します。
basic_regex& assign(
const basic_regex& right);
basic_regex& assign(
const Elem* ptr,
flag_type flags = ECMAScript);
basic_regex& assign(
const Elem* ptr,
size_type len,
flag_type flags = ECMAScript);
basic_regex& assign(
initializer_list<_Elem> IList,
flag_type flags = regex_constants::ECMAScript);
template <class STtraits, class STalloc>
basic_regex& assign(
const basic_string<Elem, STtraits, STalloc>& str,
flag_type flags = ECMAScript);
template <class InIt>
basic_regex& assign(
InIt first, InIt last,
flag_type flags = ECMAScript);
パラメーター
STtraits
文字列ソースの特徴 (traits) クラス。
STalloc
文字列ソースのアロケーター クラス。
InIt
範囲ソースの入力反復子の型。
right
コピーする Regex ソース。
ptr
コピーするシーケンスの先頭を指すポインター。
flags
コピー中に追加する構文オプション フラグ。
len/TD>
コピーするシーケンスの長さ。
str
コピーする文字列。
first
コピーするシーケンスの最初。
last
コピーするシーケンスの最後。
IList
コピーする initializer_list。
解説
いずれのメンバー関数も、*this
が保持している正規表現を、オペランド シーケンスが保持している正規表現に置き換えて、*this
を返します。
basic_regex::basic_regex
正規表現オブジェクトを構築します。
basic_regex();
explicit basic_regex(
const Elem* ptr,
flag_type flags);
explicit basic_regex(
const Elem* ptr,
size_type len,
flag_type flags);
basic_regex(
const basic_regex& right);
basic_regex(
initializer_list<Type> IList,
flag_type flags);
template <class STtraits, class STalloc>
explicit basic_regex(
const basic_string<Elem, STtraits, STalloc>& str,
flag_type flags);
template <class InIt>
explicit basic_regex(
InIt first,
InIt last,
flag_type flags);
パラメーター
STtraits
文字列ソースの特徴 (traits) クラス。
STalloc
文字列ソースのアロケーター クラス。
InIt
範囲ソースの入力反復子の型。
right
コピーする Regex ソース。
ptr
コピーするシーケンスの先頭を指すポインター。
flags
コピー中に追加する構文オプション フラグ。
len/TD>
コピーするシーケンスの長さ。
str
コピーする文字列。
first
コピーするシーケンスの最初。
last
コピーするシーケンスの最後。
IList
コピーする initializer_list。
解説
すべてのコンストラクターは、既定で構築される、RXtraits
型のオブジェクトを格納します。
1 つ目のコンストラクターは、空の basic_regex
オブジェクトを構築します。 それ以外のコンストラクターは、オペランド シーケンスで記述された正規表現を保持する basic_regex
オブジェクトを構築します。
空の basic_regex
オブジェクトは、 regex_match、 regex_search、または regex_replaceに渡されるときに、どの文字シーケンスにも一致しません。
basic_regex::flag_type
構文オプション フラグの型です。
typedef regex_constants::syntax_option_type flag_type;
解説
この型は regex_constants::syntax_option_type のシノニムです。
basic_regex::flags
構文のオプション フラグを返します。
flag_type flags() const;
解説
このメンバー関数は、basic_regex::assign メンバー関数のうち直前に呼び出された関数に渡された flag_type
引数の値を返します。これに当たる呼び出しが行われていなかった場合は、コンストラクターに渡した値が返されます。
basic_regex::getloc
格納されているロケール オブジェクトを返します。
locale_type getloc() const;
解説
メンバー関数は traits.
regex_traits::getloc()
を返します。
basic_regex::imbue
格納されているロケール オブジェクトを変更します。
locale_type imbue(locale_type loc);
パラメーター
loc
格納するロケール オブジェクト。
解説
メンバー関数は *this
を空にして、traits.
regex_traits::imbue(loc)
を返します。
basic_regex::locale_type
格納されているロケール オブジェクトの型。
typedef typename RXtraits::locale_type locale_type;
解説
この型は regex_traits::locale_typeのシノニムです。
basic_regex::mark_count
一致した部分式の数を返します。
unsigned mark_count() const;
解説
メンバー関数は、正規表現のキャプチャ グループの数を返します。
basic_regex::operator=
値を正規表現オブジェクトに代入します。
basic_regex& operator=(const basic_regex& right);
basic_regex& operator=(const Elem *str);
template <class STtraits, class STalloc>
basic_regex& operator=(const basic_string<Elem, STtraits, STalloc>& str);
パラメーター
STtraits
文字列ソースの特徴 (traits) クラス。
STalloc
文字列ソースのアロケーター クラス。
right
コピーする Regex ソース。
str
コピーする文字列。
解説
それぞれの演算子が、 *this
に保持されている正規表現を、オペランド シーケンスで記述された正規表現に置き換えてから、 *this
を返します。
basic_regex::swap
2 つの正規表現オブジェクトを交換します。
void swap(basic_regex& right) throw();
パラメーター
right
交換する正規表現オブジェクト。
解説
このメンバー関数は、 *this
と right の間で正規表現を交換します。 一定時間に実行し、例外をスローしません。
basic_regex::value_type
要素タイプ。
typedef Elem value_type;
解説
この型は、テンプレート パラメーター Elem の同意語です。
関連項目
<regex>
regex_match
regex_search
regex_replace
regex
wregex
regex_traits クラス