With the assumption, text.txt
looks like you've given:
size 5,
turn_count 3,
entity 1 ACDEF 2x2,
entity 2 BDFHC 4x5,
entity 3 CDHGF 5x5,
turn 1 2x3 4x5 5x4,
turn 2 3x3 4x4 5x3,
turn 3 3x4 4x3 5x2.
this code snippet should find those token of your interest: ACDEF
, BDFHC
, CDHGF
:
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <algorithm>
#include <regex>
using namespace std;
int main(){
string line;
vector<string> vect;
// result vector with the found string
vector<string> vectResult;
// token you want to find
// can also use array or whatever you like
const list<string> lst = {"ACDEF","BDFHC","CDHGF"};
// get your text.txt into a vector
ifstream myfile ("../src/text.txt");
if (myfile.is_open()){
while ( getline (myfile, line)){
vect.push_back(line);
}
myfile.close();
}
// constructor for the token iterator
regex_token_iterator<string::iterator> rend;
// for every token of interest
for (string x : lst){
// go through my initial vector
for(string text : vect){
// extract it from the line, only if it is founded.
if (regex_search(text, regex(x))){
regex re(x);
regex_token_iterator<string::iterator> a ( text.begin(), text.end(), re);
while (a!=rend)
// put the token of interest into the result vector
vectResult.push_back(*a++);
}
}
}
// check content of your new vector
for (string x: vectResult)
cout << x << " ";
return 0;
}
usually you'd like to change the regex the way it fits your needs.