Thread: Cannot grab strings from html tags

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    40

    Cannot grab strings from html tags

    Hi guys,

    I am using managed c++/cli. I have a trouble with my code, I cannot be able to grab the html tags from my website using with httprequest.

    Here's the html tags:

    PHP Code:
    <a href="delete.php?test=test&id=1">Delete</a> </td



    Here's the current code:

    Code:
    try
            {
                Form1 ^form1 = dynamic_cast<Form1 ^>(Owner);
                //Address of URL
                String ^URL = "http://mysite.com/members.php?user=" + form2->label1->Text + "&pass=" + form2->label2->Text;
                HttpWebRequest ^request = safe_cast<HttpWebRequest^>(WebRequest::Create(URL));
                HttpWebResponse ^response = safe_cast<HttpWebResponse^>(request->GetResponse());
                StreamReader ^reader = gcnew StreamReader(response->GetResponseStream());
                String ^str = reader->ReadToEnd();
                String ^pattern1 = form1->ListView1->SelectedItems[0]->Text + "</p><p id=\"delete\"> <a href=\"delete.php\\?delete.php?test=test&id=(.*?)\">";
                Match ^m1 = Regex::Match(str, pattern1);
                MatchCollection ^matches1 = Regex::Matches(str, pattern1);
    
                for each (Match ^x1 in matches1)
                {
                    MessageBox::Show("test 1");
                    array<String^> ^StrArr1 = x1->Value->ToString()->Split();
                    String ^test = (URL + x1->Value->ToString()->Replace(form1->ListView1->SelectedItems[0]->Text, "")->Replace("</p><p id=\"delete\"> <a href=\"", "")->Replace("\"" + ">", ""));
                    MessageBox::Show(test);
                }
             }
            catch (Exception ^ex)
             {
            }


    Do you know why and what's wrong?

    Any advice would be much appreciated.

    Thanks,
    Mark

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    This part of the regex looks wrong:

    \"delete.php\\?delete.php?test=test

    Apart from the duplicated delete.php, there's unescaped question marks and dots.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Where you're using .NET, look to leverage the HTML Agility Pack for parsing HTML documents.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    40
    Quote Originally Posted by rags_to_riches View Post
    Where you're using .NET, look to leverage the HTML Agility Pack for parsing HTML documents.
    sorry i don't know how to use it. please can you help?

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    40
    do anyone know how to do this????????????

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Did you read CornedBee's response?
    Apart from the duplicated delete.php, there's unescaped question marks and dots.
    Try this for your regex:
    Code:
    "</p><p id=\"delete\"> <a href=\"delete\.php\?test=test&id=(.*?)\">"
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting strings from HTML tags!
    By sagars1989 in forum C Programming
    Replies: 1
    Last Post: 10-16-2011, 03:12 AM
  2. Trying to grab the HTML from a Page...
    By rloveless in forum Networking/Device Communication
    Replies: 6
    Last Post: 05-05-2007, 01:03 AM
  3. I need to open a web page from c++ and grab the html.
    By rloveless in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2006, 04:12 PM
  4. Help w/ HTML Tags
    By Landroid in forum C++ Programming
    Replies: 5
    Last Post: 03-08-2005, 08:19 PM
  5. HTML tags
    By netboy in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 03-27-2002, 07:52 AM