Matt D. Webb
Matt D. Webb

Reputation: 3314

RegEx white space between elements

I am saving the html page as text where I want to pull out names from the file using RegEx. Nothing is being returned, I think because of the white space between the element. I tried to add \s* between the span and anchor. PLEASE HELP me fix it...

Here is an example of the html:

<li>
     <span class="name">
           <a href="/players/player-name.html">Stanley,&nbsp;Kyle</a>
     </span>
</li>

I am using C#, here is my code:

        static void Main(string[] args)
    {
        using (StreamReader r = new StreamReader(@"textfile.txt"))
        {

            string content = r.ReadToEnd();

            Regex r1 = new Regex("<span class=\"name\"><a.*>(.*?)</a></span>");

            MatchCollection mc = r1.Matches(content);
            foreach (Match m in mc)
            {
                Console.WriteLine(m.Groups[1].Value);
            }
        }
        Console.ReadLine();
        Console.WriteLine();

    }

Upvotes: 0

Views: 250

Answers (1)

Sabuj Hassan
Sabuj Hassan

Reputation: 39355

Regex r1 = new Regex(
  @"<span class=""name"">\s*<a.*?>(.*?)</a>\s*</span>",
  RegexOptions.Multiline
);

Upvotes: 1

Related Questions