1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> FindTextBetween(string StringBeingSearched, string OpeningStringToFind, string ClosingStringToFind);
int main()
{
vector<string> StoreTheFoundStrings;
cout << "Test 1." << endl;
StoreTheFoundStrings = FindTextBetween("<html>This is the text between the html tags.</html>", "<html>", "</html>");
if (StoreTheFoundStrings.size()>0) //If the size is >0 that means that it found at least 1 phrase between the tags
//If size is 2, it found 2 phrases between opening/closing tags etc
{
for (int i=0; i<StoreTheFoundStrings.size(); i++) //We'll loop through each element in the vector
{
cout << "Found string between tags:" << StoreTheFoundStrings[i] << endl; //Then we'll print each element the vector found
}
}
//In the first example, only one vector was found, so the for loop didn't essentially make a difference. However, we could have something like this
cout << endl << "Test 2. 3 Strings encapsulated in <html> tags." << endl;
StoreTheFoundStrings = FindTextBetween("<html>This is the text between the html tags.</html> <html>This is another string between html tags.</html> <html>And another</html>", "<html>", "</html>");
if (StoreTheFoundStrings.size()>0) //If the size is >0 that means that it found at least 1 phrase between the tags
//If size is 2, it found 2 phrases between opening/closing tags etc
{
for (int i=0; i<StoreTheFoundStrings.size(); i++) //We'll loop through each element in the vector
{
cout << "Found string between tags:" << StoreTheFoundStrings[i] << endl; //Then we'll print each element the vector found
}
}
cout << endl << "Test 3. Passing strings instead of manually typing in fields." << endl;
//Also don't forget we can pass strings instead of actually typing in the phrases
string OpeningTags = "<b>";
string ClosingTags = "</b>";
string StringContainingBoldTag = "<b>This is text between the bold tag.</b>";
StoreTheFoundStrings = FindTextBetween(StringContainingBoldTag, OpeningTags, ClosingTags);
if (StoreTheFoundStrings.size()>0) //If the size is >0 that means that it found at least 1 phrase between the tags
//If size is 2, it found 2 phrases between opening/closing tags etc
{
for (int i=0; i<StoreTheFoundStrings.size(); i++) //We'll loop through each element in the vector
{
cout << "Found string between tags:" << StoreTheFoundStrings[i] << endl; //Then we'll print each element the vector found
}
}
system("pause");
return 0;
}
vector<string> FindTextBetween(string StringBeingSearched, string OpeningStringToFind, string ClosingStringToFind)
{
vector<string> VectorStringToReturn;
string StringToSearch = StringBeingSearched;
while (true)
{
unsigned PosOfFirstString = StringToSearch.find(OpeningStringToFind);
if (PosOfFirstString == string::npos) //If Opening string contents are not found in the string being searched
{
return VectorStringToReturn;
}
unsigned PosOfSecondString = StringToSearch.find(ClosingStringToFind);
if (PosOfSecondString == string::npos) //If closing string contents are not found in the string being searched
{
return VectorStringToReturn;
}
VectorStringToReturn.resize(VectorStringToReturn.size()+1);
VectorStringToReturn[VectorStringToReturn.size()-1] = StringToSearch.substr(PosOfFirstString+OpeningStringToFind.size(),PosOfSecondString-ClosingStringToFind.size()+1-PosOfFirstString);
StringToSearch = StringToSearch.substr(PosOfSecondString+ClosingStringToFind.size(),StringToSearch.size()-1);
int x=5;
}
//cout << StringToSearch.substr(PosOfFirstString+OpeningStringToFind.size(),PosOfSecondString-ClosingStringToFind.size()+1);
//return VectorStringToReturn;
}
|