Thread: Problem with pattern matches for checked items in listview

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

    Problem with pattern matches for checked items in listview

    Hi guys,

    I need your help, I have a problem with the matches pattern for the checked items in the listview. I couldn't be able to find the solution by when I selected the checkboxes items in the listview and when I clicked on the button, the messagebox is suppose to be displaying but nothing have happens.

    What I am trying to achieve is to find the matches in pattern that I have ticked the checkboxes items in the listview, then find the strings in my website that matched with the checked items in the listview and find the html tags id from the html source, e.g: www.mysite.com/images?user=tester&id=1. Then display the messagebox.

    PHP:
    PHP Code:
    <p id="images"> <a href="images.php?id=?">Images</a></td
    The Code:
    Code:
    private: System::Void Button1_Click(System::Object^  sender, System::EventArgs^  e) {
    
    
    			 for each (ListViewItem ^item in this->listView1->CheckedItems)
    			 {
    				 try
    				 {
    					 String ^URL1 = "http://www.mysite.com/link.php?user=tester";
    					  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 + "(<p id='images'>(.*?)</p>)";
    					  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();
    						 String ^test = (URL1 + x1->Value->ToString());
    						 MessageBox::Show(test);
    					 }
    				 }
    				 catch (Exception ^ex)
    				 {
    
    				 }
    			 }
    
    		 }
    I am definately sure that the html tags has the correct tags name.

    Any idea?

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    40
    two days have passed and no one have reply. do anyone know how to fix this?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    1. have you verified that the web request is returning valid data?
    2. have you verified that item->text contains a vaild regex string?
    3. does matches1 contain any items?

    also, do the web request once, and search the response multiple times. no reason to go to the web 50 times, if the content is the same every time.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Well, first of all this isn't C++, but C++/CLI. There are good reasons to use .NET and good reasons to use native C++ but I can guarantee that you don't have a valid reason to use C++/CLI. C++/CLI combines the drawbacks of both. It's the cryptic C++ syntax combines with the inferior .NET performance.

    Anyway... where does it fail? All you told us is that "it doesn't work". I can see that you catch and silently swallow any exception so for all we know it could crash in the very first line. Have you set a breakpoint and checked what happens? How many matches are in the collection?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    40
    Quote Originally Posted by nvoigt View Post
    Well, first of all this isn't C++, but C++/CLI. There are good reasons to use .NET and good reasons to use native C++ but I can guarantee that you don't have a valid reason to use C++/CLI. C++/CLI combines the drawbacks of both. It's the cryptic C++ syntax combines with the inferior .NET performance.

    Anyway... where does it fail? All you told us is that "it doesn't work". I can see that you catch and silently swallow any exception so for all we know it could crash in the very first line. Have you set a breakpoint and checked what happens? How many matches are in the collection?

    I am using C++/CLI, so I don't see anything wrong with between unmanaged C++ and managed C++/CLI.

    However, I have put the breakpoint in this line:

    Code:
    String ^pattern1 = listView1->SelectedItems[0]->SubItems[1]->Text + "(<p id='images'>(.*?)</p>)";
    I can see that I have got an undefined value.

    When I tried this:

    Code:
    String ^ pattern1 = listView1->SelectedItems[0]->SubItems[1]->Text;

    I have found my matched strings from the http request and the listview without using the regex pattern, so when I tried this:

    Code:
    String ^ pattern1 = listView1->SelectedItems[0]->SubItems[1]->Text + "(<p id='images'>(.*?)</p>)";

    I did not get the returned strings, so what i am trying to do is to find the matched strings that I selected in the listview and find the html tags called images, then extract the link from the tags, e.g:
    Code:
    <p id="images"> <a href="images.php?id=1">Images</a>
    I want the return strings to be like this:

    PHP Code:
    www.mysite.com/images.php?id=

    Any idea how I can do this?

    Thanks in advance.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    have you considered parsing the HTML document as HTML instead of searching it with a regular expression? then you can grab all the <p> tags (probably with an xpath query) and examine their attributes.

  7. #7
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    I'm not a regex expert and I don't have C++/CLI at home, but here's what I would do:

    Take your regex and data and try an online regex tester if matches are generated at all. Maybe your regex has errors.

    If all is fine, put both into fixed strings inside a test application (ConsoleApplication1...) and see if matches are generated.

    You need to break your problem into many smaller steps until you know which fails and which still works. Then look for a solution.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    40
    Quote Originally Posted by Elkvis View Post
    have you considered parsing the HTML document as HTML instead of searching it with a regular expression? then you can grab all the <p> tags (probably with an xpath query) and examine their attributes.
    no i have never been using to parsing the html document. do you know how to use it?

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    have a look at this link

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    40
    Quote Originally Posted by Elkvis View Post
    have a look at this link
    That wont work and it won't do anything so you are not making a sense for what i didn't asked for.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    40
    Quote Originally Posted by mark103 View Post
    That wont work and it won't do anything so you are not making a sense for what i didn't asked for.
    Anyway, I have nearly found the solution. I have changed the value in the regex pattern and I have pointed the break point in this line:

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

    Here's the value of the pattern1:

    Code:
    "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:

    PHP Code:
    <p id="images"> <a href="images.php?id=1">Images</a
    Do you 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.
    Last edited by mark103; 02-24-2012 at 03:48 PM.

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    40
    3 days have passed and nobody have reply. do anyone know how to fix this?

  13. #13
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Did you

    Take your regex and data and try an online regex tester if matches are generated at all. Maybe your regex has errors.

    If all is fine, put both into fixed strings inside a test application (ConsoleApplication1...) and see if matches are generated.
    ?

    If so, can you post this test application so we have something to work with except theories?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to check menu items if it checked is true or false?
    By mark103 in forum Windows Programming
    Replies: 1
    Last Post: 04-28-2011, 02:19 PM
  2. Double-Checked Locking pattern issue
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2008, 04:29 AM
  3. questions regarding listview items
    By Bleech in forum Windows Programming
    Replies: 7
    Last Post: 08-26-2006, 02:26 PM
  4. Moving items in a ListView
    By Cactus_Hugger in forum Windows Programming
    Replies: 1
    Last Post: 01-18-2006, 09:40 PM
  5. ListView Items
    By Smoose777 in forum C# Programming
    Replies: 1
    Last Post: 06-21-2003, 12:10 PM