Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

basic_regex::basic_regex

Construct the regular expression object.

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 class for a string source.

STalloc

Allocator class for a string source.

InIt

Input iterator type for a range source.

right

Regex source to copy.

ptr

Pointer to beginning of sequence to copy.

flags

Syntax option flags to add while copying.

len/TD>

Length of sequence to copy.

str

String to copy.

first

Beginning of sequence to copy.

last

End of sequence to copy.

IList

The initializer_list to copy.

All constructors store a default-constructed object of type RXtraits.

The first constructor constructs an empty basic_regex object. The other constructors construct a basic_regex object that holds the regular expression described by the operand sequence.

An empty basic_regex object does not match any character sequence when passed to regex_match Function, regex_search Function, or regex_replace Function.

 

// std_tr1__regex__basic_regex_construct.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' } };
    cout << "match(\"abc\", \"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", "abc") == true

Header: <regex>

Namespace: std

Show:
© 2015 Microsoft