Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. regular expression in c# [modified]

regular expression in c# [modified]

Scheduled Pinned Locked Moved C#
regexcsharphtmlcomsysadmin
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    benjamin yap
    wrote on last edited by
    #1

    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\] != '>')
    
    V P 2 Replies Last reply
    0
    • B benjamin yap

      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\] != '>')
      
      V Offline
      V Offline
      vivasaayi
      wrote on last edited by
      #2

      If I were u I, I will change the StockPriceTag string to "<root><span class=\"pr\">\n<span id=\"ref_705173_l\">26.83</span> </root>". Then load it in an XML Document. Where you can simple read the value of Span tag. Cheers.

      1 Reply Last reply
      0
      • B benjamin yap

        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\] != '>')
        
        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups