954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with Array

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:

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


Thanks in advance.

farooqaaa
Posting Whiz in Training
295 posts since Jul 2008
Reputation Points: 61
Solved Threads: 71
 
String html = getSource();
List<Group> newGroups = new List<Group>();
r = new Regex("class=lnk href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
for (m = r.Match(html); m.Success; m = m.NextMatch())
{ 
      MessageBox.Show("Link" + m.Groups[1].Value); 
//new modified code
newGroups.Add(m.Groups[index]); //not 1 as you say
}


Read my comments in the code I may got you wrong.

Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

Thanks.

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

I've modified the code, read the comments:

String html = getSource();
               // I want a list of "Strings"
                List<String> links = new List<String>();
                r = new Regex("class=lnk href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                for (m = r.Match(html); m.Success; m = m.NextMatch())
                {
                    //MessageBox.Show("Link" + m.Groups[1].Value);
                    links.Add(m.Groups[1].Value);
                }

// This messagebox only show 1 link while the above messagebox showed all the links (that matched the regex).

                for (int i = 0; i < links.Count; i++)
                {
                    MessageBox.Show(links[i]);
                }


I hope you'll get me now.

Thanks again for your help.

farooqaaa
Posting Whiz in Training
295 posts since Jul 2008
Reputation Points: 61
Solved Threads: 71
 

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.

Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

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.

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


Thanks!

farooqaaa
Posting Whiz in Training
295 posts since Jul 2008
Reputation Points: 61
Solved Threads: 71
 

Can you please give me the link? to know what's this about.

Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

The regex scans for "href=link.com>Link" and gets the link.

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

http://msdn.microsoft.com/en-us/library/t9e807fx(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.

farooqaaa
Posting Whiz in Training
295 posts since Jul 2008
Reputation Points: 61
Solved Threads: 71
 
String html = richTextBox1.Text;
            List<string> matches = new List<string>();
            Regex r = new Regex("href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            Match m;// = r.Match(
            for (m = r.Match(html); m.Success; m = m.NextMatch())
            {
                matches.Add(m.Value);
                MessageBox.Show(m.Value);
                //new modified code
                //newGroups.Add(m.Groups[index]); //not 1 as you say
            }
Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

Thanks a million :). That code worked!

Thanks again!

farooqaaa
Posting Whiz in Training
295 posts since Jul 2008
Reputation Points: 61
Solved Threads: 71
 

That's what you needed?? or it popup unneeded string?

Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

No. I am not using the popup. I only used it to find out whether links were capture or not.

And it popup NEEDED strings :D

BTW, I am working on a bot. It visits a website (using webbrowser control) and then gets all the available links. And then visits each one and so on.

Thanks :)

farooqaaa
Posting Whiz in Training
295 posts since Jul 2008
Reputation Points: 61
Solved Threads: 71
 

You're welcome, just made sure it meets your need.

Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You