syntax_option_type — std::regex_constants::syntax_option_type
syntax_option_type — std::regex_constants::syntax_option_type
syntax_option_type은 정규식 문법을 선택·제어하는 옵션 타입이에요. C++11에서 도입됐어요. <regex> 헤더에 있어요.
std::basic_regex를 만들 때 어떤 문법(ECMAScript, POSIX, grep 등)을 쓸지 지정해요.
출처: cppreference
본문
// <regex> 헤더, C++11
using syntax_option_type = /* implementation-defined */; // (1)
constexpr syntax_option_type icase = /* unspecified */;
constexpr syntax_option_type nosubs = /* unspecified */;
constexpr syntax_option_type optimize = /* unspecified */;
// ...
주요 옵션
| 옵션 | 설명 |
|---|---|
icase |
대소문자 무시 매칭 |
nosubs |
부분 일치(sub-match)를 저장하지 않음 |
optimize |
매칭 속도를 최적화 (구성 비용 증가) |
collate |
로케일 콜레이트 규칙 적용 |
ECMAScript |
ECMAScript 문법 (기본값) |
basic |
POSIX 기본 정규식 (BRE) |
extended |
POSIX 확장 정규식 (ERE) |
awk |
awk 문법 |
grep |
grep 문법 |
egrep |
egrep 문법 |
사용 예
#include <regex>
// 기본: ECMAScript
std::regex re1("a.b");
// 대소문자 무시
std::regex re2("hello", std::regex::icase);
// POSIX 확장 문법
std::regex re3("a|b", std::regex::extended);
// 문법과 옵션 조합
std::regex re4("(ab)+", std::regex::ECMAScript | std::regex::icase);
문법 간 차이
// ECMAScript에서 + 는 1회 이상
std::regex re("a+");
// POSIX basic 에서는 \( ... \) + 등이 달라짐
std::regex rebasic("a\\+", std::regex::basic);
특징
std::regex_constants네임스페이스와std::regex멤버 상수로 접근 가능.- 비트 OR로 여러 옵션을 조합할 수 있어요.
- 문법 옵션(
ECMAScript,extended등)은 보통 하나만 선택돼요.
// std::regexSearch 등에 문법 옵션 상속
std::regex re("\\w+", std::regex::icase | std::regex::optimize);
syntax_option_type은 C++ 정규식의 "어떤 문법과 동작으로 매칭할지"를 결정하는 핵심 옵션이에요. 기본 ECMAScript 외에 POSIX/grep 등이 필요할 때 지정해요.