我在MinGW上试用正则表达式(参见代码示例),但我得到了
“what:regex_error; code:error_escape:无效的转义字符或尾随转义”
消息表示抛出了error_escape异常。我不明白为什么因为代码在Visual Studio 2013(Windows 7)和Mac OSX环境中工作。
Sublime Text中的编译和链接命令是:
"cmd": ["g++", "-std=c++11", "$file_name", "-o", "${file_base_name}.exe", "-lm", "-Wall", "&&", "${file_base_name}.exe"]
#include <iostream>
#include <string>
#include <regex>
#include <iterator>
std::string parseCode(std::regex_constants::error_type etype) {
switch (etype) {
case std::regex_constants::error_collate:
return "error_collate: invalid collating element request";
case std::regex_constants::error_ctype:
return "error_ctype: invalid character class";
case std::regex_constants::error_escape:
return "error_escape: invalid escape character or trailing escape";
case std::regex_constants::error_backref:
return "error_backref: invalid back reference";
case std::regex_constants::error_brack:
return "error_brack: mismatched bracket([ or ])";
case std::regex_constants::error_paren:
return "error_paren: mismatched parentheses(( or ))";
case std::regex_constants::error_brace:
return "error_brace: mismatched brace({ or })";
case std::regex_constants::error_badbrace:
return "error_badbrace: invalid range inside a { }";
case std::regex_constants::error_range:
return "erro_range: invalid character range(e.g., [z-a])";
case std::regex_constants::error_space:
return "error_space: insufficient memory to handle this regular expression";
case std::regex_constants::error_badrepeat:
return "error_badrepeat: a repetition character (*, ?, +, or {) was not preceded by a valid regular expression";
case std::regex_constants::error_complexity:
return "error_complexity: the requested match is too complex";
case std::regex_constants::error_stack:
return "error_stack: insufficient memory to evaluate a match";
default:
return "";
}
}
void regexMatchExample()
{
if (std::regex_match ("subject", std::regex("(sub)(.*)") ))
std::cout << "string literal matched\n";
const char cstr[] = "subject";
std::string s ("subject");
std::regex e ("(sub)(.*)");
if (std::regex_match (s,e))
std::cout << "string object matched\n";
if ( std::regex_match ( s.begin(), s.end(), e ) )
std::cout << "range matched\n";
std::cmatch cm; // same as std::match_results<const char*> cm;
std::regex_match (cstr,cm,e);
std::cout << "string literal with " << cm.size() << " matches\n";
std::smatch sm; // same as std::match_results<string::const_iterator> sm;
std::regex_match (s,sm,e);
std::cout << "string object with " << sm.size() << " matches\n";
std::regex_match ( s.cbegin(), s.cend(), sm, e);
std::cout << "range with " << sm.size() << " matches\n";
// using explicit flags:
std::regex_match ( cstr, cm, e, std::regex_constants::match_default );
std::cout << "the matches were: ";
for (unsigned i=0; i<sm.size(); ++i) {
std::cout << "[" << sm[i] << "] ";
}
std::cout << std::endl;
}
void regexSearchExample()
{
std::string s ("this subject has a submarine as a subsequence");
std::smatch m;
try {
std::regex e("\\b(sub)([^ ]*)"); // matches words beginning by "sub"
std::cout << "Target sequence: " << s << std::endl;
std::cout << "Regular expression: /\\b(sub)([^ ]*)/" << std::endl;
std::cout << "The following matches and submatches were found:" << std::endl;
while (std::regex_search(s,m,e)) {
for (auto x:m) std::cout << x << " ";
std::cout << std::endl;
s = m.suffix().str();
}
} catch (std::regex_error &e) {
std::cout << "what: " << e.what() << "; code: " << parseCode(e.code()) << std::endl;
}
}
int main ()
{
std::cout << "######## regexMatchExample ############" << std::endl;
regexMatchExample();
std::cout << "######## regexSearchExample ############" << std::endl;
regexSearchExample();
return 0;
}