Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
below is the string value i am getting and i want to store the second address value in a variable and n case it only return one address the first one then it shouldn't store this address
Server:  resolver1.opendns.com
Address:  202.62.333.112

Non-authoritative answer:
Name:    myip.opendns.com
Address:  19.40.22.46


i only want to store the second address in a variable which is 19.40.22.46

What I have tried:

C++
istringstream is3(result);
const string Pattern = "Address:";

//result is the variable in which i wil get above output
// string line;

// const size_t Len = Pattern.length();
string ipchk;
// string line98;

while (getline(is3, line98))
{
    if (line98.find(Pattern) != string::npos)
    {

        line98.erase(0, 39);
        ipchk = line98.erase(line98.size() - 1);

        //ipchk = line98.substr(Len);
        // return line98.substr(Len);
        Ipv4 = ipchk;
        break;
    }
}
IpAddress=Ipv4;
Posted
Updated 2-Mar-20 2:18am
v2
Comments
Richard MacCutchan 2-Mar-20 5:06am    
And?
What have you tried?
Where are you stuck?
Member 12899279 2-Mar-20 6:39am    
istringstream is3(result);
const string Pattern = "Address:";

//result is the variable in which i wil get above output
// string line;

// const size_t Len = Pattern.length();
string ipchk;
// string line98;

while (getline(is3, line98))
{
if (line98.find(Pattern) != string::npos)
{

line98.erase(0, 39);
ipchk = line98.erase(line98.size() - 1);

//ipchk = line98.substr(Len);
// return line98.substr(Len);
Ipv4 = ipchk;
break;
}
}
IpAddress=Ipv4;

this
Richard MacCutchan 2-Mar-20 6:46am    
So what is the problem?
Stefan_Lang 2-Mar-20 7:20am    
That is the kind of info you should have added to your question with the [Improve question] button at the lower right of your question.

This time I did it for you, and restored some formatting to make it readable too.

That said, this code is incomplete, and not very helpful to understand what your problem is. If you're stuck, you should put a little more effort into explaining where.

1 solution

A regular expression would be perfect to isolate the second result:
C++
#include <regex>

std::regex r("^Address:\s+((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4})$");
std::smatch match;
std::string address;

if (std::regex_match(result, match, r))
{
   address = match[match.size() > 1 ? 1 : 0]; // The second result, if more than one, or the first result
}
 
Share this answer
 
v2
Comments
Member 12899279 2-Mar-20 9:01am    
its not even going inside the loop i think regex is incorrect btw thanks for trying
Stefan_Lang 2-Mar-20 10:15am    
I'm not very familiar with Regex, but I think you are supposed to concatenate all lines and let the Regex extract the resulting the addresses from the multiline string. No loop required.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900