gpt4 book ai didi

C++ 11 正则表达式错误

转载 作者:太空狗 更新时间:2023-10-29 21:02:48 24 4
gpt4 key购买 nike

<分区>

C++ Primer 第 5 版中的示例代码:17.3.3。使用正则表达式库

主文件main.cpp:

#include <iostream>
#include "regexcase.h"
using namespace std;

int main() {
using_regex();
return 0;
}

头文件regexcase.h:

#ifndef REGEXCASE_H_
#define REGEXCASE_H_

#include <regex>
#include <string>

void using_regex();
std::string parseCode(std::regex_constants::error_type etype);

#endif /* REGEXCASE_H_ */

源文件regexcase.cpp:

#include "regexcase.h"
#include <iostream>
using namespace std;

void using_regex() {
// look for words that violate a well-known spelling rule of thumb, "i before e, except after c":
// find the characters ei that follow a character other than c
string pattern("[^c]ei");
// we want the whole word in which our pattern appears
pattern = "[a-zA-Z]*" + pattern + "[a-zA-Z]*"; //[a-zA-Z]* [[:alpha:]]*
try {
regex r(pattern, regex_constants::extended); // construct a regex to find pattern // , regex_constants::extended
smatch results; // define an object to hold the results of a search
// define a string that has text that does and doesn't match pattern
string test_str = "receipt freind theif receive";
// use r to find a match to pattern in test_str
if (regex_search(test_str, results, r)) // if there is a match
cout << results.str() << endl; // print the matching word
else
cout << "no match for " << pattern << endl;
} catch (regex_error &e) {
cout << "what: " << e.what() << "; code: " << parseCode(e.code()) << endl;
}
}

string parseCode(regex_constants::error_type etype) {
switch (etype) {
case regex_constants::error_collate:
return "error_collate: invalid collating element request";
case regex_constants::error_ctype:
return "error_ctype: invalid character class";
case regex_constants::error_escape:
return "error_escape: invalid escape character or trailing escape";
case regex_constants::error_backref:
return "error_backref: invalid back reference";
case regex_constants::error_brack:
return "error_brack: mismatched bracket([ or ])";
case regex_constants::error_paren:
return "error_paren: mismatched parentheses(( or ))";
case regex_constants::error_brace:
return "error_brace: mismatched brace({ or })";
case regex_constants::error_badbrace:
return "error_badbrace: invalid range inside a { }";
case regex_constants::error_range:
return "erro_range: invalid character range(e.g., [z-a])";
case regex_constants::error_space:
return "error_space: insufficient memory to handle this regular expression";
case regex_constants::error_badrepeat:
return "error_badrepeat: a repetition character (*, ?, +, or {) was not preceded by a valid regular expression";
case regex_constants::error_complexity:
return "error_complexity: the requested match is too complex";
case regex_constants::error_stack:
return "error_stack: insufficient memory to evaluate a match";
default:
return "";
}
}

调用 using_regex(); 的输出是 what: regex_error;代码:error_brack:括号不匹配([或])

似乎正则表达式无法解析括号。

请参阅 this question 中的答案,我使用 regex_constants::extended 来初始化 regex 对象,然后是 regex r(pattern, regex_constants::extended);

那么输出是no match for [[:alpha:]]*[^c]ei[[:alpha:]]*

似乎正则表达式不能匹配模式。

然后我使用 [a-zA-Z]* 替换字符类 [[:alpha:]]* (使用 regex_constants::extended 仍然设置)。输出仍然是 no match for [a-zA-Z]*[^c]ei[a-zA-Z]*

平台:windows

使用的工具:Eclipse for C/C++MinGW(g++ --版本:g++ 4.7.2)

编辑:感谢@sharth,添加主文件以完成代码。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com