Locked patterm problem?

  • 24. února 2012 21:47
     
      Obsahuje kód

    Hi guys,

    I need your help, I am using C++/CLI to code my program. I have got a problem with the regex pattern. I couldn't be able to extract the hyperlink from my php page while I find the matches pattern that I selected on the listview items.

    Code:

    <var>private</var>: System::Void Button1_Click(System::Object^  sender, System::EventArgs^  e) {


    <var>for</var> each (ListViewItem ^item in <var>this</var>->listView1->CheckedItems)
    {
    <var>try</var>
    {
    String ^URL1 = <kbd>"http://www.mysite.com/link.php?user=tester"</kbd>;
    HttpWebRequest ^request1 = safe_cast<HttpWebRequest^>(WebRequest::Create(URL1));
    HttpWebResponse ^response1 = safe_cast<HttpWebResponse^>(request1->GetResponse());
    StreamReader ^reader1 = gcnew StreamReader(response1->GetResponseStream());
    String ^str1 = reader1->ReadToEnd();
    String ^pattern1 = item->Text + <kbd>"(<p id='images'>(.*?)</p>)"</kbd>;
    MatchCollection ^matches1 = Regex::Matches(str1, pattern1);
    Match ^m1 = Regex::Match(str1, pattern1);


    <var>for</var> each (Match ^x1 in matches1)
    {
    array<String^> ^StrArr1 = x1->Value->ToString()->Split();
    String ^test = (URL1 + x1->Value->ToString());
    MessageBox::Show(test);
    }
    }
    <var>catch</var> (Exception ^ex)
    {

    }
    }

    }

    I am definitely sure that the html tags has the correct tags name.

    However, I have put the breakpoint in this line:

    Match ^m1 = Regex::Match(str1, pattern1);

    I can see the value for pattern1 so here it is:

    "my strings 1<p id='images'>(.*?)</p>"

    It have found the matches with the html tag, but it doesn't extract the id. So here's the html tags:

    <p id="images"> <a href="images.php?id=1">Images</a>  

    Do anyone know how I can extract the id in the images tags from the html source?

    Any idea how I can do this?

    Thanks in advance.

    • Přesunutý Helen Zhao 28. února 2012 3:30 (From:Visual C++ General)
    •  

Všechny reakce

  • 27. února 2012 17:45
     
     
    3 days have passed and nobody have reply. do anyone know how to fix this?
  • 28. února 2012 3:29
     
     

    Hi Mark106,

    Welcome to the MSDN Forum.

    According to your description, I'd like to move this thread to "Regular Expressions Forum" for better support. And there is a similar one in that forum. Please refer to it for help.

    Thanks for your understanding.
    Best regards,


    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us

  • 28. února 2012 15:54
     
     Odpovědět

    You should use the HTML Agility Pack to accomplish that. Also, I am not sure what 'my string 1' is there for? Try also getting the ? out of the group like so:

    <p id="images">(.*)?


    John Grove, Senior Software Engineer http://www.digitizedschematic.com/

  • 29. února 2012 2:12
     
     

    Sorry, I don't know how to use HTML Agility Pack. Please can you tell me how i can use it?

    As I am showing an example of what strings I get from my php page that I extracted the information from the database.

  • 29. února 2012 14:58
     
     

    Are you familiar with the Xml classes from System.Xml? It is almost the same thing. If not, it is not something I can teach you in a thread. BTW, I have no idea what 'my string 1' is. This shows up in your pattern but not in what you are capturing so I am not certain you are giving us the full story.

    The pattern "my strings 1<p id='images'>(.*?)</p>" will never capture --> <p id="images"> <a href="images.php?id=1">Images</a> 


    John Grove, Senior Software Engineer http://www.digitizedschematic.com/

  • 13. března 2012 22:21
     
     

    Hi guys,

    I am working on my program to read the html tags using with httprequest. When i set the timer, it connect to my site via httprequest and it will read the whole tags from the php source when I am trying to compare between <p id='mystrings1'> and the <span id="mystrings2">Enabled">.

    here's what the messagebox display:
    [PHP]

    <p id='mystrings1'>user data 1</p><p id="images"> <a href="images.php?id=1">Images</a></td> | <a href="http://myhotlink.com">Link</a> </td> | <a href="delete.php?id=1">Delete</a> </td> | <span id="mystrings2">Enabled</td>
    [/PHP]

    Here's the form code:

    [CODE]

    #include "StdAfx.h"
    #include "Form1.h"


    using namespace MyProject;

    System::Void Form1::timer1_Tick(System::Object^  sender, System::EventArgs^  e)
    {
        timer1->Enabled = false;
        timer1->Stop();

        try
        {
            String ^URL1 = "http://mysite.com/myscript.php?user=test&pass=test";
            HttpWebRequest ^request1 = safe_cast<HttpWebRequest^>(WebRequest::Create(URL1));
            HttpWebResponse ^response1 = safe_cast<HttpWebResponse^>(request1->GetResponse());
            StreamReader ^reader1 = gcnew StreamReader(response1->GetResponseStream());
            String ^str1 = reader1->ReadToEnd();
            String ^pattern1 = "(<p id='mystrings1'>(.*?)</p>(.*?)<span id=\"mystrings2\">Enabled</td>)";
            MatchCollection ^matches1 = Regex::Matches(str1, pattern1);
            Match ^m1 = Regex::Match(str1, pattern1);

            for each (Match ^x1 in matches1)
            {
                array<String^> ^StrArr1 = x1->Value->ToString()->Split();
                MessageBox::Show(x1->Value->ToString());
            }
        }
        catch (Exception ^ex)
        {
        }
    }

    [/CODE]

    What I am trying to do is to find the tags called mystrings1 and mystrings2 with the value "enabled". I want to see if it have find the matches in the same line as the mystrings1 and mystrings2, then ignore the other tags and display the messagebox just like this:

    [CODE]<p id='mystrings1'>user data 1 <span id="mystrings2">Enabled</td>[/CODE]

    what my program are doing is they are looking to compare the two tags between mystrings1 and mystrings2, then it display the whole tags from the php source.


    Please can someone tell me how i can split the whole tags when i am trying to compare with the two tags?

    Thanks,
    Mark

    • Přesunutý Helen Zhao 15. března 2012 7:31 (From:Visual C++ General)
    • Sloučený Lie YouModerator 16. března 2012 7:07 Merged them to keep good discuss.
    •  
  • 14. března 2012 19:47
     
     

    Load this as a XML string and then use xome XML parser to parse your string which is easy and fast.

    Thanks


    Rupesh Shukla

  • 14. března 2012 21:43
     
     

    Hi Mark,

    If it's really simple you may be better off just parsing it by hand by moving through the string character by character and doing string compares from each position for the token you are looking for.  If the job is more difficult you may be able to use something like:

    HTMLCXX


    Tom

  • 15. března 2012 7:31
     
     

    Hi Mark,

    According to your description, I'd like to move this thread to "Common Language Runtime Forum" for better support, where more experts live.

    Thanks for your understanding and active participation in the MSDN Forum.
    Have a nice day!


    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us

  • 12. dubna 2012 10:01