First, I have to tell you that without MSDN and other good online resources, I would
have never been able to do anything useful... Many Thanks.
So I searched here and other places for my problem... and I can't seem to find anything quite like it .. altough a quite obvious task...
I have a program that reads in a string the html sourcecode of a website (www.whatismyip.com) here, hehe not to name it !! .
I need to find a way to: copy what is contained between (for instance) displaycopy(' AND ')
(witch is my ip !!) and store it into a string...
loop parsing the string I even read about converting the html in an xml to use the (nodes )
I'm a n00b .. well at XML at the verry least... =)
But.. Never UnderEstimate The Curiosity of a N00b!
So please help me solve this (quite easy ! ) task..
see the screenshot for the (view) of my string (the html source)
here displayed in a messagebox...
http://madlogik.mine.nu:999/mystring.jpg
So how do I (extract) my ip into a string for later use from this other string (here displayed in the above messagebox screenshot)
thanks again
-Mad-

[C#2005] Parsing data from and to a string
StephenMas
Probably the easiest way is to use something called RegEx which is short for Regular Expressions. It's kind of a cryptic way of looking for patterns.
Overview http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemtextregularexpressionsregexclasstopic.asp
A Great place to get started http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpgenref/html/cpconRegularExpressionsLanguageElements.asp
The below code Downloads the webpage using the WebClient and stores the page in PageContents.
We then create an Regex expression that will find your IP. (NOte this will NOT work for IPv6. If you want you should be able to replace this with any regex you find on the web or www.regexlib.com if you really care to get the 1% of the people that are using ipv6.
We then Match it against the P{ageContents and check to see if we got a match. If we didn't we return null otherwise we return the value because that "should" hold the IP.
Hope this helps.
String GetIP(){
System.Net.
WebClient Webbie = new System.Net.WebClient(); String PageContents = Webbie.DownloadString("http://www.whatismyip.com");System.Text.RegularExpressions.
Regex IPReg = new System.Text.RegularExpressions.Regex(@"(.[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)");System.Text.RegularExpressions.
Match TmpMatch = IPReg.Match(PageContents); if ((TmpMatch==null) || (TmpMatch.Success==false)){
// no match was found. return null;}
else{
return TmpMatch.Value;}
}
icewill
You could probably use mine unless you are using .NET 1.0 or 1.1
Web Client is a class Microsoft created to simplify certain tasks. For example one of the most common tasks is to make a request for a web page and get its content. The DownloadString that I did in my code basically takes your 20-30 lines of code and puts it in one. Can save you alot of time and make your code look alot more succint
Good luck.
AndersBank
Thanks MarcD!
Here is my (adaptation) with a bit more work on the buffer... I hope not for nothing
Its working like a charm like this for my Wan ip button (puts ip in textfield) Thanks again!
private void btn_wanip_Click(object sender, EventArgs e){
// My Wan Ip Button.. // used to build entire input StringBuilder sb = new StringBuilder(); // used on each read operation byte[] buf = new byte[8192]; // prepare the web page we will be asking for HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://www.whatismyip.com"); // execute the request HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// we will read data via the response stream Stream resStream = response.GetResponseStream(); string tempString = null; int count = 0; do{
// fill the buffer with datacount = resStream.Read(buf, 0, buf.Length);
// make sure we read some data if (count != 0){
// translate from bytes to ASCII texttempString =
Encoding.ASCII.GetString(buf, 0, count); // continue building the stringsb.Append(tempString);
}
}
while (count > 0); // any more data to read // print out page source //MessageBox.Show(sb.ToString()); // find the IP address in the string .....System.Net.
WebClient Webbie = new System.Net.WebClient(); //String PageContents = Webbie.DownloadString("http://www.whatismyip.com"); // .. replaced by my lenghty code! String PageContents = sb.ToString(); //my string with all the html source!System.Text.RegularExpressions.
Regex IPReg = new System.Text.RegularExpressions.Regex(@"(.[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)");System.Text.RegularExpressions.
Match TmpMatch = IPReg.Match(PageContents); if ((TmpMatch == null) || (TmpMatch.Success == false)){
//nothing had numbers.numbers.numbers.numbers !!(no ip) MessageBox.Show("no match was found.");}
else{
//we have a wan ip!!txt_wanip.Text = TmpMatch.ToString().TrimStart();
}
}
Ashok Ojha
I was worried about the buffer size.. but Thanks! it really makes it easier to read as well!!
-Max