Hello! I am currently having quite the problem while developing my software. I cannot seem to find a decent way to find all values that match the delimeters. for example, here is the data in the form of a string:
This is just an example of the delimiters I would like to use. At first, I tried using regex, but I couldn’t seem to find a way to remove the tags themselves, and to get the values. Also, with regex, I had no way of looping through the results. I have attempted to use string::find, but I couldn’t seem to find a way to make it work. Any help is appreciated. Thanks, Drew.
#include <iostream>
#include <vector>
#include <sstream>
#include <string>
usingnamespace std;
// ADDS any appropriately delimited strings to an EXISTING collection
// Requires a PERFECT match (including case)
void delimited( string text, string before, string after, vector<string> &collection )
{
int p = 0, q;
while ( true )
{
p = text.find( before, p ); if ( p == string::npos ) return;
p += before.size();
q = text.find( after, p ); if ( q == string::npos ) return;
collection.push_back( text.substr( p, q - p ) );
}
}
int main()
{
stringstream fakeFile( "[Data]Important[/Data][Data]Copy this[/Data]\n""[Bob]Not important[/Bob]\n""[Bob]Leave till tomorrow[/Bob][Data]Ignore[/Data]" );
vector<string> items;
string line;
while ( getline( fakeFile, line ) ) delimited( line, "[Data]", "[/Data]", items );
cout << "Required items are:\n";
for ( string s : items ) cout << s << '\n';
}
Required items are:
Important
Copy this
Ignore
- Must match the delimiters PRECISELY (including case) - easily fixed if necessary
- More efficient to pass longer strings by constant reference than by value, but trying to keep the argument list simple here.
For some strange reason, I can't seem to get the above code to compile. I keep getting that quoted is not a member of namespace std? Also, A little confused about how I can store each result as a seperate string in an array of strings.