Programming/c++

[c++] basic_regex class

owls 2023. 1. 20. 14:58
728x90

1. Parameter

Elem The type of element to find a match for.
RXtraits Traits class for elements.

 

2. Described

클래스 템플릿은 정규식을 보유하는 개체를 설명합니다. 이 클래스 템플릿의 개체는 템플릿 함수 regex_match, regex_search 및 regex_replace 전달할 수 있습니다. 또한 정규식과 일치하는 텍스트를 검색하기 위해 적절한 텍스트 문자열 인수를 전달합니다. 

이 클래식 템플릿에는 char 타입 원소인 regex와 wchar_t 타입 원소인 wregex를 사용하는 두 개의 전문 클래스 템플릿이 있습니다.

 

3.  Member

member default
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 collation regex_constants::collate
public static const flag_type ECMAScript regex_constants::ECMAScript
public static const flag_type basic regex_constants::basic
Extend public static const flag_type 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 Traits  

 

4. Constructor

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);

 

4. Member Function

member  function Description
assign Assign values to regular expression objects.
flags Returns syntax option flags.
getloc Returns the stored locale object.
imbue Change the stored locale object.
mark_count  Returns the number of matching subexpressions.
swap Swaps two regular expression objects.

 

 

5. Operator

Operator Description
operator== Assign values ​​to regular expression objects.

 

 

6.예제

#include <iostream>
#include <regex>

int main(){
    regex::value_type elem = 'x';
    regex::flag_type flag = regex::grep;

    elem = elem;
    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;
    return 0;
}
728x90