#include <iostream>
#include <string>
std::string get_str_between_two_str(std::string s, std::string start_delim,
std::string stop_delim)
{
unsigned first_delim_pos = s.find(start_delim);
unsigned end_pos_of_first_delim = first_delim_pos + start_delim.length();
unsigned last_delim_pos = s.find(stop_delim);
return s.substr(end_pos_of_first_delim,
last_delim_pos - end_pos_of_first_delim);
}
int main() {
// Want to extract _0_192.168.1.18_
std::string s = "_STARTDELIMITER_0_192.168.1.18_STOPDELIMITER_";
std::string s2 = "ABC123_STARTDELIMITER_0_192.168.1.18_STOPDELIMITER_XYZ345";
std::string start_delim = "_STARTDELIMITER";
std::string stop_delim = "STOPDELIMITER_";
std::cout << get_str_between_two_str(s, start_delim, stop_delim) << std::endl;
std::cout << get_str_between_two_str(s2, start_delim, stop_delim) << std::endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgogCnN0ZDo6c3RyaW5nIGdldF9zdHJfYmV0d2Vlbl90d29fc3RyKHN0ZDo6c3RyaW5nIHMsIHN0ZDo6c3RyaW5nIHN0YXJ0X2RlbGltLAoJCXN0ZDo6c3RyaW5nIHN0b3BfZGVsaW0pCnsKCXVuc2lnbmVkIGZpcnN0X2RlbGltX3BvcyA9IHMuZmluZChzdGFydF9kZWxpbSk7Cgl1bnNpZ25lZCBlbmRfcG9zX29mX2ZpcnN0X2RlbGltID0gZmlyc3RfZGVsaW1fcG9zICsgc3RhcnRfZGVsaW0ubGVuZ3RoKCk7Cgl1bnNpZ25lZCBsYXN0X2RlbGltX3BvcyA9IHMuZmluZChzdG9wX2RlbGltKTsKIAoJcmV0dXJuIHMuc3Vic3RyKGVuZF9wb3Nfb2ZfZmlyc3RfZGVsaW0sCgkJCWxhc3RfZGVsaW1fcG9zIC0gZW5kX3Bvc19vZl9maXJzdF9kZWxpbSk7Cn0KIAppbnQgbWFpbigpIHsKICAgIC8vIFdhbnQgdG8gZXh0cmFjdCBfMF8xOTIuMTY4LjEuMThfCglzdGQ6OnN0cmluZy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