error_type — std::regex_constants::error_type

error_type — std::regex_constants::error_type

std::error_type(정확히는 std::regex_constants::error_type)는 정규식 라이브러리 오류의 종류를 나타내는 타입이에요. C++11에서 도입됐어요. <regex> 헤더에 있어요.

정규식 구성·매칭 시 발생하는 오류 코드를 나타내요.

출처: cppreference

본문

// <regex> 헤더, C++11
using error_type = /* implementation-defined */;   // (1)

constexpr error_type error_collate =    /* unspecified */;
constexpr error_type error_ctype =      /* unspecified */;
constexpr error_type error_escape =     /* unspecified */;
constexpr error_type error_backref =    /* unspecified */;
// ... 등 오류 상수들

오류 상수

상수 설명
error_collate 잘못된 콜레이트 요소
error_ctype 잘못된 문자 클래스
error_escape 잘못된 이스케이프
error_backref 잘못된 역참조
error_brack 잘못된 대괄호
error_paren 잘못된 괄호
error_brace 잘못된 중괄호
error_badbrace 잘못된 반복 범위
error_range 잘못된 범위
error_space 메모리 부족
error_badrepeat 잘못된 반복
error_complexity 복잡도 초과
error_stack 스택 부족

사용 예

이 오류 코드는 std::regex 객체를 만들 때 잘못된 패턴이면 std::regex_error 예외로 던져지고, 그 예외의 code()로 조회할 수 있어요.

#include <regex>
#include <iostream>

try {
    std::regex re("(");   // 잘못된 패턴
} catch (const std::regex_error& e) {
    std::cout << "regex error code: " << static_cast<int>(e.code()) << '\n';
    // e.code()는 regex_constants::error_type
    if (e.code() == std::regex_constants::error_paren) {
        std::cout << "괄호 오류\n";
    }
}

error_type은 예외의 종류를 세밀하게 식별해서, 패턴의 어떤 부분이 잘못됐는지 진단하는 데 유용해요.

더 알아보기 (Learn more)

cppreference