使用 Regex 在 C++ 中查找并打印括号之间的数字

Find and print the number between the parentheses in C++ using Regex

我正在使用此代码在 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] 的第 2 次迭代中。 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;
}

更新: 现在我可以读取 numbers.using 字符串。

但我的文件太大 Visual studio 崩溃了。

我想要一种在文件中进行全局搜索的方法。

首先,您应该将 regex e("\[(\d+)\]") 修复为 regex e("\[(\d+)]")(或者 regex e(R"(\[(\d+)])"),如果您的 C++ 环境支持原始字符串文字)。

然后,您需要获取多个匹配项,而不仅仅是您当前正在做的所有捕获组内容。使用 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';
}
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);
}