regular expression in c# [modified]
-
Hi, i want to retrieve some content from a webpage. The method below retrieve the webpage content (in html).
public string GetWebpageContent(string StockQuote) { //Store url + desired Stock Quote //E.g. NYSE, MSFT string serverURL = "http://www.google.com/finance?q=" + StockQuote; //Create a HTTP request based on url HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverURL); //Retrieve byte response from server base on the above request WebResponse response = request.GetResponse(); //Retrieve content using StreamReader StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII); //Read content string retVal = reader.ReadToEnd(); //Close the stream reader reader.Close(); //Return data return retVal; }
Below method retrieve the stockprice in the web content. the html i wan to read consist this
<div>After Hours: <span class=bld id="ref_21913_el">10.60</span>
I wan to retrieve the value 10.60 The id="ref_21913_el" is not a fix value, so i have to read base on
<span class=bld....
public string GetStockPrice() { //Used to store the reverse Stock Price StringBuilder ReversePrice = new StringBuilder(); //Store the actual price string Price = ""; //Match the webpage content with the following pattern MatchCollection StockQuote = Regex.Matches(urlContent, "", RegexOptions.Singleline); //Retrieve the first matched result string StockPriceTag = StockQuote\[0\].Value; //"<span class=\\"pr\\">\\n<span id=\\"ref\_705173\_l\\">26.83</span>" //Remove closing </span> tag string WithoutEndSpanTag = StockPriceTag.Remove(StockPriceTag.Length - 7, 7); //<span class=\\"pr\\">\\n<span id=\\"ref\_705173\_l\\">26.83 //Traverse through individual characters starting from the back for (int i = WithoutEndSpanTag.Length - 1; i >= 0; i--) { //Covert string to individual characters char\[\] AllChar = WithoutEndSpanTag.ToCharArray(); //Check current item is not '>' if (AllChar\[i\] != '>')
-
Hi, i want to retrieve some content from a webpage. The method below retrieve the webpage content (in html).
public string GetWebpageContent(string StockQuote) { //Store url + desired Stock Quote //E.g. NYSE, MSFT string serverURL = "http://www.google.com/finance?q=" + StockQuote; //Create a HTTP request based on url HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverURL); //Retrieve byte response from server base on the above request WebResponse response = request.GetResponse(); //Retrieve content using StreamReader StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII); //Read content string retVal = reader.ReadToEnd(); //Close the stream reader reader.Close(); //Return data return retVal; }
Below method retrieve the stockprice in the web content. the html i wan to read consist this
<div>After Hours: <span class=bld id="ref_21913_el">10.60</span>
I wan to retrieve the value 10.60 The id="ref_21913_el" is not a fix value, so i have to read base on
<span class=bld....
public string GetStockPrice() { //Used to store the reverse Stock Price StringBuilder ReversePrice = new StringBuilder(); //Store the actual price string Price = ""; //Match the webpage content with the following pattern MatchCollection StockQuote = Regex.Matches(urlContent, "", RegexOptions.Singleline); //Retrieve the first matched result string StockPriceTag = StockQuote\[0\].Value; //"<span class=\\"pr\\">\\n<span id=\\"ref\_705173\_l\\">26.83</span>" //Remove closing </span> tag string WithoutEndSpanTag = StockPriceTag.Remove(StockPriceTag.Length - 7, 7); //<span class=\\"pr\\">\\n<span id=\\"ref\_705173\_l\\">26.83 //Traverse through individual characters starting from the back for (int i = WithoutEndSpanTag.Length - 1; i >= 0; i--) { //Covert string to individual characters char\[\] AllChar = WithoutEndSpanTag.ToCharArray(); //Check current item is not '>' if (AllChar\[i\] != '>')
-
Hi, i want to retrieve some content from a webpage. The method below retrieve the webpage content (in html).
public string GetWebpageContent(string StockQuote) { //Store url + desired Stock Quote //E.g. NYSE, MSFT string serverURL = "http://www.google.com/finance?q=" + StockQuote; //Create a HTTP request based on url HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverURL); //Retrieve byte response from server base on the above request WebResponse response = request.GetResponse(); //Retrieve content using StreamReader StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII); //Read content string retVal = reader.ReadToEnd(); //Close the stream reader reader.Close(); //Return data return retVal; }
Below method retrieve the stockprice in the web content. the html i wan to read consist this
<div>After Hours: <span class=bld id="ref_21913_el">10.60</span>
I wan to retrieve the value 10.60 The id="ref_21913_el" is not a fix value, so i have to read base on
<span class=bld....
public string GetStockPrice() { //Used to store the reverse Stock Price StringBuilder ReversePrice = new StringBuilder(); //Store the actual price string Price = ""; //Match the webpage content with the following pattern MatchCollection StockQuote = Regex.Matches(urlContent, "", RegexOptions.Singleline); //Retrieve the first matched result string StockPriceTag = StockQuote\[0\].Value; //"<span class=\\"pr\\">\\n<span id=\\"ref\_705173\_l\\">26.83</span>" //Remove closing </span> tag string WithoutEndSpanTag = StockPriceTag.Remove(StockPriceTag.Length - 7, 7); //<span class=\\"pr\\">\\n<span id=\\"ref\_705173\_l\\">26.83 //Traverse through individual characters starting from the back for (int i = WithoutEndSpanTag.Length - 1; i >= 0; i--) { //Covert string to individual characters char\[\] AllChar = WithoutEndSpanTag.ToCharArray(); //Check current item is not '>' if (AllChar\[i\] != '>')
Here's a Regular Expression that will match your span and put the price in a named group:
\<span class=bld id=\"[^\"]*\"\>(?'Price'[\d\.]*)\</span\>
Read up on what Regular Expressions can do.