regex_traits 类

描述用于匹配的元素的特征。

语法

template<class Elem>
class regex_traits

参数

Elem
要描述的字符元素类型。

注解

此类模板描述 Elem 类型的各种正则表达式特征。 此类模板 basic_regex 类 使用此信息来操作 Elem 类型的元素。

每个 regex_traits 对象包含 regex_traits::locale 类型的对象,该对象由它的一些成员函数使用。 默认区域设置是一份 regex_traits::locale()副本。 成员函数 imbue 替换区域设置对象,而成员函数 getloc 返回区域设置对象的副本。

构造函数

构造函数 说明
regex_traits 构造 对象。

Typedef

类型名称 说明
char_class_type 字符类指示符的类型。
char_type 元素的类型。
locale_type 存储的区域设置对象的类型。
size_type 序列长度的类型。
string_type 元素字符串的类型。

成员函数

成员函数 说明
getloc 返回存储的区域设置对象。
imbue 更改存储的区域设置对象。
isctype 类成员资格测试。
length 返回以 null 结尾的序列的长度。
lookup_classname 将序列映射到字符类。
lookup_collatename 将序列映射到排序规则元素。
transform 转换为等效顺序序列。
transform_primary 转换为不区分大小写的顺序等效序列。
translate 转换为等效的匹配元素。
translate_nocase 转换为不区分大小写的等效匹配序列。
value 将元素转换为数字值。

要求

标头:<regex>

命名空间: std

示例

// std__regex__regex_traits.cpp
// compile with: /EHsc
#include <regex>
#include <iostream>

typedef std::regex_traits<char> Mytr;
int main()
    {
    Mytr tr;

    Mytr::char_type ch = tr.translate('a');
    std::cout << "translate('a') == 'a' == " << std::boolalpha
        << (ch == 'a') << std::endl;

    std::cout << "nocase 'a' == 'A' == " << std::boolalpha
        << (tr.translate_nocase('a') == tr.translate_nocase('A'))
        << std::endl;

    const char *lbegin = "abc";
    const char *lend = lbegin + strlen(lbegin);
    Mytr::size_type size = tr.length(lbegin);
    std::cout << "length(\"abc\") == " << size <<std::endl;

    Mytr::string_type str = tr.transform(lbegin, lend);
    std::cout << "transform(\"abc\") < \"abc\" == " << std::boolalpha
        << (str < "abc") << std::endl;

    const char *ubegin = "ABC";
    const char *uend = ubegin + strlen(ubegin);
    std::cout << "primary \"ABC\" < \"abc\" == " << std::boolalpha
        << (tr.transform_primary(ubegin, uend) <
            tr.transform_primary(lbegin, lend))
        << std::endl;

    const char *dig = "digit";
    Mytr::char_class_type cl = tr.lookup_classname(dig, dig + 5);
    std::cout << "class digit == d == " << std::boolalpha
        << (cl == tr.lookup_classname(dig, dig + 1))
        << std::endl;

    std::cout << "'3' is digit == " <<std::boolalpha
        << tr.isctype('3', tr.lookup_classname(dig, dig + 5))
        << std::endl;

    std::cout << "hex C == " << tr.value('C', 16) << std::endl;

// other members
    str = tr.lookup_collatename(dig, dig + 5);

    Mytr::locale_type loc = tr.getloc();
    tr.imbue(loc);

    return (0);
    }
translate('a') == 'a' == true
nocase 'a' == 'A' == true
length("abc") == 3
transform("abc") < "abc" == false
primary "ABC" < "abc" == false
class digit == d == true
'3' is digit == true
hex C == 12

regex_traits::char_class_type

字符类指示符的类型。

typedef T8 char_class_type;

备注

类型用于指定字符类的未指定类型的同义词。 可以使用 | 运算符指定属于由操作数指定的类并集的字符类,从而组合此类型的值。

regex_traits::char_type

元素的类型。

typedef Elem char_type;

备注

Typedef 是模板参数 Elem的同义词。

regex_traits::getloc

返回存储的区域设置对象。

locale_type getloc() const;

注解

此成员函数返回存储的 locale 对象。

regex_traits::imbue

更改存储的区域设置对象。

locale_type imbue(locale_type loc);

参数

loc
要存储的区域设置对象。

注解

成员函数将 loc 复制到存储的 locale 对象,并返回存储的 locale 对象的以前值的副本。

regex_traits::isctype

类成员资格测试。

bool isctype(char_type ch, char_class_type cls) const;

参数

ch
要测试的元素。

cls
要测试的类。

备注

仅当字符 ch 属于 cls 指定的字符类时,成员函数才返回 true。

regex_traits::length

返回以 null 结尾的序列的长度。

static size_type length(const char_type *str);

参数

str
以 null 结尾的序列。

注解

此静态成员函数返回 std::char_traits<char_type>::length(str)

regex_traits::locale_type

存储的区域设置对象的类型。

typedef T7 locale_type;

注解

Typedef 是封装区域设置的类型的同义词。 在专业 regex_traits<char>regex_traits<wchar_t> 中,它是 std::locale的同义词。

regex_traits::lookup_classname

将序列映射到字符类。

template <class FwdIt>
char_class_type lookup_classname(FwdIt first, FwdIt last) const;

参数

first
要查找的序列的开头。

last
要查找的序列的结尾。

备注

该成员函数返回一个值,该值指定由其自变量指向的字符序列命名的字符类。 该值不依赖序列中字符的大小写。

专用化 regex_traits<char> 识别名称 "d""s""w""alnum""alpha""blank""cntrl""digit""graph""lower""print""punct""space""upper""xdigit",均不区分大小写。

专用化 regex_traits<wchar_t> 识别名称 L"d"L"s"L"w"L"alnum"L"alpha"L"blank"L"cntrl"L"digit"L"graph"L"lower"L"print"L"punct"L"space"L"upper"L"xdigit",均不区分大小写。

regex_traits::lookup_collatename

将序列映射到排序规则元素。

template <class FwdIt>
string_type lookup_collatename(FwdIt first, FwdIt last) const;

参数

first
要查找的序列的开头。

last
要查找的序列的结尾。

注解

此成员函数将返回一个字符串对象,它包含对应于序列 [first, last)的排序规则元素;如果该序列不是有效的排序规则元素,则返回空字符串。

regex_traits::regex_traits

构造 对象。

regex_traits();

备注

构造函数构造其存储的 locale 对象初始化为默认区域设置的对象。

regex_traits::size_type

序列长度的类型。

typedef T6 size_type;

备注

typedef 是无符号整数类型的同义词。 在专业 regex_traits<char>regex_traits<wchar_t> 中,它是 std::size_t的同义词。

typedef 是 std::size_t的同义词。

regex_traits::string_type

元素字符串的类型。

typedef basic_string<Elem> string_type;

注解

typedef 是 basic_string<Elem>的同义词。

regex_traits::transform

转换为等效顺序序列。

template <class FwdIt>
string_type transform(FwdIt first, FwdIt last) const;

参数

first
要转换的序列的开头。

last
要转换的序列的结尾。

注解

此成员函数返回它使用转换规则生成的字符串,转换规则依赖于 locale 对象。 对于由迭代器范围 [first1, last1)[first2, last2)指定的两个字符序列,如果由迭代器范围 transform(first1, last1) < transform(first2, last2) 指定的字符序列排序在由迭代器范围 [first1, last1) 指定的字符序列之前,则 [first2, last2)

regex_traits::transform_primary

转换为不区分大小写的顺序等效序列。

template <class FwdIt>
string_type transform_primary(FwdIt first, FwdIt last) const;

参数

first
要转换的序列的开头。

last
要转换的序列的结尾。

注解

此成员函数返回它使用转换规则生成的字符串,转换规则依赖于 locale 对象。 对于由迭代器范围 [first1, last1)[first2, last2)指定的两个字符序列,如果由迭代器范围 transform_primary(first1, last1) < transform_primary(first2, last2) 指定的字符序列排序在由迭代器范围 [first1, last1) 指定的字符序列之前(不考虑大小写或重音字符),则 [first2, last2)

regex_traits::translate

转换为等效的匹配元素。

char_type translate(char_type ch) const;

参数

ch
要转换的元素。

备注

此成员函数返回一个字符,它通过使用取决于存储的 locale 对象的转换规则而生成。 对于两个 char_type 对象( ch1ch2),只有 translate(ch1) == translate(ch2)ch1 匹配(在其中一个在正则表达式定义中出现且另一个在区分区域设置匹配的目标序列中的相应位置出现时),才为 ch2

regex_traits::translate_nocase

转换为不区分大小写的等效匹配序列。

char_type translate_nocase(char_type ch) const;

参数

ch
要转换的元素。

注解

此成员函数返回一个字符,它通过使用取决于存储的 locale 对象的转换规则而生成。 对于两个 char_type 对象( ch1ch2),只有当 translate_nocase(ch1) == translate_nocase(ch2)ch1 的匹配条件为其中一个在正则表达式中出现且另一个在不区分大小写匹配的目标序列中的相应位置出现时,才为 ch2

regex_traits::value

将元素转换为数字值。

int value(Elem ch, int radix) const;

参数

ch
要转换的元素。

radix
要使用的算术基数。

备注

该成员函数将返回由基数 radix 中字符 ch 表示的值,或者如果 ch 不是基数 radix 中的有效数字,则返回 -1。 只有使用 radix 参数 8、10 或 16 才能调用该函数。

另请参阅

<regex>
regex_constants 类
regex_error 类
<regex> 函数
regex_iterator 类
<regex> 运算符
regex_token_iterator 类
<regex> typedefs
regex_traits<char> 类
regex_traits<wchar_t> 类