在c++primer 第五版第649页出现了表17.7--正则表达式错误类型。它到底是什么? 到底怎么用?让我们利用源码一探究竟:
源码:
1 namespace regex_constants 2 { 3 _GLIBCXX_BEGIN_NAMESPACE_VERSION 4 5 /** 6 * @name 5.3 Error Types 7 */ 8 //@{ 9 10 enum error_type 11 { 12 _S_error_collate, 13 _S_error_ctype, 14 _S_error_escape, 15 _S_error_backref, 16 _S_error_brack, 17 _S_error_paren, 18 _S_error_brace, 19 _S_error_badbrace, 20 _S_error_range, 21 _S_error_space, 22 _S_error_badrepeat, 23 _S_error_complexity, 24 _S_error_stack, 25 }; 26 27 /** The expression contained an invalid collating element name. */ 28 constexpr error_type error_collate(_S_error_collate); 29 30 /** The expression contained an invalid character class name. */ 31 constexpr error_type error_ctype(_S_error_ctype); 32 33 /** 34 * The expression contained an invalid escaped character, or a trailing 35 * escape. 36 */ 37 constexpr error_type error_escape(_S_error_escape); 38 39 /** The expression contained an invalid back reference. */ 40 constexpr error_type error_backref(_S_error_backref); 41 42 /** The expression contained mismatched [ and ]. */ 43 constexpr error_type error_brack(_S_error_brack); 44 45 /** The expression contained mismatched ( and ). */ 46 constexpr error_type error_paren(_S_error_paren); 47 48 /** The expression contained mismatched { and } */ 49 constexpr error_type error_brace(_S_error_brace); 50 51 /** The expression contained an invalid range in a {} expression. */ 52 constexpr error_type error_badbrace(_S_error_badbrace); 53 54 /** 55 * The expression contained an invalid character range, 56 * such as [b-a] in most encodings. 57 */ 58 constexpr error_type error_range(_S_error_range); 59 60 /** 61 * There was insufficient memory to convert the expression into a 62 * finite state machine. 63 */ 64 constexpr error_type error_space(_S_error_space); 65 66 /** 67 * One of <em>*?+{</em> was not preceded by a valid regular expression. 68 */ 69 constexpr error_type error_badrepeat(_S_error_badrepeat); 70 71 /** 72 * The complexity of an attempted match against a regular expression 73 * exceeded a pre-set level. 74 */ 75 constexpr error_type error_complexity(_S_error_complexity); 76 77 /** 78 * There was insufficient memory to determine whether the 79 * regular expression could match the specified character sequence. 80 */ 81 constexpr error_type error_stack(_S_error_stack); 82 83 //@} 84 _GLIBCXX_END_NAMESPACE_VERSION 85 }
解读:
error_collate是枚举类型error_type的常量表达式,在编译器就确定好了error_type的值:_S_error_collate 对应到整型常量就是 0.
error_ctype是枚举类型error_type的常量表达式,在编译器就确定好了error_type的值:_S_error_ctype,对应到整型常量就是 1.
其它就是依此类推。
error_collate | 0 |
error_ctype | 1 |
error_escape | 2 |
error_backref | 3 |
error_brack | 4 |
error_paren | 5 |
error_brace | 6 |
error_badbrace | 7 |
error_range | 8 |
error_space | 9 |
error_badrepeat | 10 |
error_complexity | 11 |
error_stack | 12 |
如何使用?
其实不用刻意使用,在正则表达式错误的时候,就会报出相应的错误。
例如:
1 try { 2 regex r("[[:alnum:]]+\.cpp|cxx|cc)$", regex::icase); 3 } catch (regex_error e) 4 { cout << e.what() << " code: " << e.code() << endl; }
这个时候第二行少了一个左括号,应该会爆出error_paren的错误,对应的数值是5,编译发现
因此验证通过。
综上所述
error_collate不是类类型,不是函数,而是一个整型常量0~12,用来表示regex_error的错误具体是什么样子的。