Need help with Array

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2008
Posts: 38
Reputation: farooqaaa is an unknown quantity at this point 
Solved Threads: 1
farooqaaa's Avatar
farooqaaa farooqaaa is offline Offline
Light Poster

Need help with Array

 
0
  #1
Jul 18th, 2009
By using Regex I've got links from an HTML Source.

But now I want to save these links into a new array so that I can later use them. I mean, I want all the "m.Groups[]" in a new array.

Here's my code:

  1. String html = getSource();
  2. r = new Regex("class=lnk href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
  3. for (m = r.Match(html); m.Success; m = m.NextMatch())
  4. {
  5. MessageBox.Show("Link" + m.Groups[1].Value);
  6. }

Thanks in advance.
Last edited by farooqaaa; Jul 18th, 2009 at 3:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Need help with Array

 
0
  #2
Jul 18th, 2009
  1. String html = getSource();
  2. List<Group> newGroups = new List<Group>();
  3. r = new Regex("class=lnk href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
  4. for (m = r.Match(html); m.Success; m = m.NextMatch())
  5. {
  6. MessageBox.Show("Link" + m.Groups[1].Value);
  7. //new modified code
  8. newGroups.Add(m.Groups[index]); //not 1 as you say
  9. }

Read my comments in the code I may got you wrong.
Last edited by Ramy Mahrous; Jul 18th, 2009 at 4:49 pm.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 38
Reputation: farooqaaa is an unknown quantity at this point 
Solved Threads: 1
farooqaaa's Avatar
farooqaaa farooqaaa is offline Offline
Light Poster

Re: Need help with Array

 
0
  #3
Jul 18th, 2009
Thanks.

The "index" doesn't work. It say "index" doesn't exist.

I've modified the code, read the comments:

  1. String html = getSource();
  2. // I want a list of "Strings"
  3. List<String> links = new List<String>();
  4. r = new Regex("class=lnk href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
  5. for (m = r.Match(html); m.Success; m = m.NextMatch())
  6. {
  7. //MessageBox.Show("Link" + m.Groups[1].Value);
  8. links.Add(m.Groups[1].Value);
  9. }
  10.  
  11. // This messagebox only show 1 link while the above messagebox showed all the links (that matched the regex).
  12.  
  13. for (int i = 0; i < links.Count; i++)
  14. {
  15. MessageBox.Show(links[i]);
  16. }

I hope you'll get me now.

Thanks again for your help.
Last edited by farooqaaa; Jul 18th, 2009 at 5:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Need help with Array

 
0
  #4
Jul 18th, 2009
Sure it won't work as index not defined but what I need to say is you everytime add the same Group instance which in the index 1 of the array of Group.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 38
Reputation: farooqaaa is an unknown quantity at this point 
Solved Threads: 1
farooqaaa's Avatar
farooqaaa farooqaaa is offline Offline
Light Poster

Re: Need help with Array

 
0
  #5
Jul 18th, 2009
Hmm. But that's what MSDN gave me.

Anyways, I did this in VB and its working properly. Here's the code:

Can you look at that code and get ideas and then get me the C# code, please? Will be really appreciated.

  1. Dim r As New Regex("class=lnk href\s*=\s*(?:""(?<1>[^""]*)""|(?<1>\S+))", RegexOptions.IgnoreCase Or RegexOptions.Compiled)
  2. Dim m As Match
  3. Dim matches As New List(Of String) // this is the new array
  4. // The code below code works fine
  5. For Each m In r.Matches(HTML CODE)
  6. matches.Add(m.Groups(1).Value)
  7. Next

Thanks!
Last edited by farooqaaa; Jul 18th, 2009 at 5:42 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Need help with Array

 
0
  #6
Jul 18th, 2009
Can you please give me the link? to know what's this about.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 38
Reputation: farooqaaa is an unknown quantity at this point 
Solved Threads: 1
farooqaaa's Avatar
farooqaaa farooqaaa is offline Offline
Light Poster

Re: Need help with Array

 
0
  #7
Jul 18th, 2009
The regex scans for "<a href=link.com>Link</a>" and gets the link.

Here's link to MSDN, I am using this code:

http://msdn.microsoft.com/en-us/libr...fx(VS.71).aspx

The code they gave me works very great for me. But I don't know how to save the matches in a new array. That's all I want to do.....Save the matches in a new array.
Last edited by farooqaaa; Jul 18th, 2009 at 5:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Need help with Array

 
1
  #8
Jul 18th, 2009
  1. String html = richTextBox1.Text;
  2. List<string> matches = new List<string>();
  3. Regex r = new Regex("href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
  4. Match m;// = r.Match(
  5. for (m = r.Match(html); m.Success; m = m.NextMatch())
  6. {
  7. matches.Add(m.Value);
  8. MessageBox.Show(m.Value);
  9. //new modified code
  10. //newGroups.Add(m.Groups[index]); //not 1 as you say
  11. }
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 38
Reputation: farooqaaa is an unknown quantity at this point 
Solved Threads: 1
farooqaaa's Avatar
farooqaaa farooqaaa is offline Offline
Light Poster

Re: Need help with Array

 
0
  #9
Jul 18th, 2009
Thanks a million . That code worked!

Thanks again!
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Need help with Array

 
0
  #10
Jul 18th, 2009
That's what you needed?? or it popup unneeded string?
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC