#include <string>
#include <iostream>
std::string extract(const std::string& str, char beg, char end)
{
std::size_t begPos ;
if ( (begPos = str.find(beg)) != std::string::npos )
{
std::size_t endPos ;
if ( (endPos = str.find(end, begPos)) != std::string::npos && endPos != begPos+1 )
return str.substr(begPos+1, endPos-begPos-1) ;
}
return std::string() ;
}
int main()
{
std::string original("This is the string of text $Iwantthis+notneeded text, end of the string.") ;
std::cout << extract(original, '$', '+') << '\n' ;
}
I2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPGlvc3RyZWFtPgoKc3RkOjpzdHJpbmcgZXh0cmFjdChjb25zdCBzdGQ6OnN0cmluZyYgc3RyLCBjaGFyIGJlZywgY2hhciBlbmQpCnsKICAgIHN0ZDo6c2l6ZV90IGJlZ1BvcyA7CiAgICBpZiAoIChiZWdQb3MgPSBzdHIuZmluZChiZWcpKSAhPSBzdGQ6OnN0cmluZzo6bnBvcyApCiAgICB7CiAgICAgICAgc3RkOjpzaXplX3QgZW5kUG9zIDsKICAgICAgICBpZiAoIChlbmRQb3MgPSBzdHIuZmluZChlbmQsIGJlZ1BvcykpICE9IHN0ZDo6c3RyaW5nOjpucG9zICYmIGVuZFBvcyAhPSBiZWdQb3MrMSApCiAgICAgICAgICAgIHJldHVybiBzdHIuc3Vic3RyKGJlZ1BvcysxLCBlbmRQb3MtYmVnUG9zLTEpIDsKICAgIH0KCiAgICByZXR1cm4gc3RkOjpzdHJpbmcoKSA7Cn0KCmludCBtYWluKCkKewogICAgc3RkOjpzdHJpbmcgb3JpZ2luYWwoIlRoaXMgaXMgdGhlIHN0cmluZyBvZiB0ZXh0ICRJd2FudHRoaXMrbm90bmVlZGVkIHRleHQsIGVuZCBvZiB0aGUgc3RyaW5nLiIpIDsKICAgIHN0ZDo6Y291dCA8PCBleHRyYWN0KG9yaWdpbmFsLCAnJCcsICcrJykgPDwgJ1xuJyA7Cn0K