2

I am using this code to find the number between the parentheses in C++.

I want that number to be extracted out from a huge file which contain these type of data:

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]         ***********                   ***********                   ***********                   ***********

///// In the 2nd iteration when the [554] is found. the m.size() does not reset to zero that cause an error. How can i solve it? How i search it globally from the whole file the numbers in parenthesis [###]

#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;
}

Update: Now i am able to read the numbers.using string.

But my file is huge enough that Visual studio crashes.

I wanted a method that searches globally in a file.

Masood Salik
  • 119
  • 1
  • 1
  • 10

2 Answers2

2

First, you should fix the regex e("\[(\d+)\]") as regex e("\\[(\\d+)]") (or regex e(R"(\[(\d+)])") if your C++ environment supports raw string literals).

Then, you need to obtain multiple matches, not just all the capturing group contents what you are currently doing. Use an std::sregex_iterator() and access Group 1 contents via m[1].str().

See 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';
}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • I am using Visual Studio 2010. When i use std::regex r("\\[(\\d+)]"); When the code execute this , code crashes and give an error. I tries many regex expressions but most of them does not work. – Masood Salik Sep 17 '16 at 06:03
  • 1
    Try `r("\\[(\\d+)\\]");` – Wiktor Stribiżew Sep 17 '16 at 06:15
  • Thanks it works. Now the problem i am facing is that string that i am reading is too large that Visual Studio terminate the operation. So i need a method that of global search in a text file. My data is in text file. I have just added few lines in my question. Is their any way ? And i still didnt understand that why visual studio does not support some correct regex expressions. – Masood Salik Sep 17 '16 at 10:57
  • I am using while (! myfile.eof() ) { getline (myfile,s);} to get the string from file and then use regex iterator to find the numbers. – Masood Salik Sep 17 '16 at 18:12
  • Yes, that is the most efficient way to deal with text files: read line by line and process each separately. I doubt the issue is with the regex. See [*Fast textfile reading in c++*](http://stackoverflow.com/questions/17925051/fast-textfile-reading-in-c) – Wiktor Stribiżew Sep 17 '16 at 20:41
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);
}
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • This code-only answer could be improved by adding some text explaining what the code is doing and how it solves the problem. -- [From Review](https://stackoverflow.com/review/low-quality-posts/13661474) – Ryan Bemrose Sep 14 '16 at 01:13