943,670 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 756
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 18th, 2009
0

Need help with Array

Expand Post »
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:

C# Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 61
Solved Threads: 71
Posting Whiz in Training
farooqaaa is offline Offline
283 posts
since Jul 2008
Jul 18th, 2009
0

Re: Need help with Array

C# Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jul 18th, 2009
0

Re: Need help with Array

Thanks.

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

I've modified the code, read the comments:

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 61
Solved Threads: 71
Posting Whiz in Training
farooqaaa is offline Offline
283 posts
since Jul 2008
Jul 18th, 2009
0

Re: Need help with Array

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.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jul 18th, 2009
0

Re: Need help with Array

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.

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 61
Solved Threads: 71
Posting Whiz in Training
farooqaaa is offline Offline
283 posts
since Jul 2008
Jul 18th, 2009
0

Re: Need help with Array

Can you please give me the link? to know what's this about.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jul 18th, 2009
0

Re: Need help with Array

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.
Reputation Points: 61
Solved Threads: 71
Posting Whiz in Training
farooqaaa is offline Offline
283 posts
since Jul 2008
Jul 18th, 2009
1

Re: Need help with Array

C# Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jul 18th, 2009
0

Re: Need help with Array

Thanks a million . That code worked!

Thanks again!
Reputation Points: 61
Solved Threads: 71
Posting Whiz in Training
farooqaaa is offline Offline
283 posts
since Jul 2008
Jul 18th, 2009
0

Re: Need help with Array

That's what you needed?? or it popup unneeded string?
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: self-aware strings
Next Thread in C# Forum Timeline: Replace the Contents of the Array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC