我正在使用此代码查找C ++中括号之间的数字。
我希望从包含这些类型数据的大文件中提取该数字:
SUCCESSFUL CANDIDATES ARE INDICATED WITHIN PARANTHESIS AGAINST THEIR ROLL NUMBER AND THE EXTRA MARKS GIVEN [MAXIMUM FIVE MARKS] TO RAISE THEIR GRADES IN HARDSHIP CASES ARE INDICATED WITH PLUS[+] SIGN AND
GRACE MARKS CASES ARE INDICATED WITH CARET[+] SIGN
600023[545] 600024[554] 600031[605] 600052[560] ***********
Grade : D
Govt. Degree Boys College, Surjani Town
600060[877] *********** *********** *********** ***********
/////在第二次迭代中找到[554]。 m.size()不会重置为零,从而导致错误。我怎么解决呢?我如何从整个文件中全局搜索括号[###]
中的数字#include <iostream>
#include <fstream>
#include <string>
#include<conio.h>
#include<regex>
using namespace std;
int main () {
ifstream myfile;
myfile.open("test.txt") ;
if(!myfile)
{
cout<<"The file you entered is not present"<<endl;
}
string str;
if(myfile.is_open())
cout<<"file is open";
else
cout<<"file is close";
string output;
regex e("\[(\d+)\]");
smatch m;
while (!myfile.eof()) {
myfile >> str;
cout<<"O="<<str<<endl;
bool match=regex_search(str,m,e);
cout<<"m.size() ="<<m.size()<<endl;
int n=0;
for( n=0;n<m.size();n++)
{
cout<<"m["<<n<<"] :str() =" <<m[n].str()<<endl;
}
}
getch();
myfile.close();
return 0;
}
更新: 现在我能够读取数字。使用字符串。
但我的文件非常庞大,Visual Studio崩溃了。
我想要一个在文件中全局搜索的方法。
答案 0 :(得分:0)
首先,如果您的C ++环境支持原始字符串文字,则应将regex e("\[(\d+)\]")
修复为regex e("\\[(\\d+)]")
(或regex e(R"(\[(\d+)])")
。
然后,您需要获取多个匹配,而不仅仅是所有捕获组内容您当前正在执行的操作。使用std::sregex_iterator()
并通过m[1].str()
访问第1组内容。
请参阅this C++ demo:
std::regex r("\\[(\\d+)]");
std::string s = "SUCCESSFUL CANDIDATES ARE INDICATED WITHIN PARANTHESIS AGAINST THEIR ROLL NUMBER AND THE EXTRA MARKS GIVEN [MAXIMUM FIVE MARKS] TO RAISE THEIR GRADES IN HARDSHIP CASES ARE INDICATED WITH PLUS[+] SIGN AND\nGRACE MARKS CASES ARE INDICATED WITH CARET[+] SIGN\n\n\n600023[545] 600024[554] 600031[605] 600052[560] ***********\n\nGrade : D\nGovt. Degree Boys College, Surjani Town\n\n\n600060[877] *********** *********** *********** ***********";
for(std::sregex_iterator i = std::sregex_iterator(s.begin(), s.end(), r);
i != std::sregex_iterator();
++i)
{
std::smatch m = *i;
std::cout << m[1].str() << '\n';
}
答案 1 :(得分:0)
std::string::size_t loc = str.find('[');
while (loc != std::string::npos) {
++loc;
std::string::size_t end = str.find(']', loc);
if (end == std::string::npos)
break;
std::cout << str.substr(loc, end - loc);
loc = str.find('[', end);
}