<regex>

std::regex_error

class regex_error : public runtime_error { /* ... */ };
Regex 異常
此異常型別的物件由 regex 庫的元素丟擲。

它繼承自標準異常 runtime_error,並有一個特殊的公共成員函式,code,它返回一個特定型別的程式碼regex_constants::error_type,具體取決於丟擲它的錯誤型別。

flagerror
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沒有足夠的記憶體來確定正則表示式是否能匹配指定的字元序列。

成員函式

explicit regex_error (regex_constants::error_type ecode);
使用錯誤程式碼 ecode 構建一個 regex 異常。
regex_constants::error_type code() const;
返回構造時使用的錯誤程式碼。
regex_constants::error_type是一個列舉型別,用於唯一標識 regex 庫中的錯誤(有關更多資訊,請參閱 regex_constants)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// regex_error
#include <iostream>
#include <regex>

int main ()
{
  try {
     std::regex myregex ("*");
  } catch (std::regex_error& e) {
     if (e.code() == std::regex_constants::error_badrepeat)
       std::cerr << "Repeat was not preceded by a valid regular expression.\n";
     else std::cerr << "Some other regex exception happened.\n";
  }
  return 0;
}

輸出(stderr):
Repeat was not preceded by a valid regular expression.


另見