AlesSvetina
AlesSvetina

Reputation: 463

Find element with any Id with regex

I have a string:

<span id="5" class="diamond" name="diamond">*</span>&#160;<span id="-1" class="diamond" name="diamond">*</span>

I want too find all the spans ( * ) with any id. How do I do this with regex? what should the "pattern" variable be?

string input = vsebina;
string pattern = "/<span id=\"-1\" class=\"diamond\" name=\"diamond\">*<\\/span>";

MatchCollection matches = Regex.Matches(input, pattern);
Console.WriteLine("Matches found: {0}", matches.Count);

string[] allDiamonds = new string[matches.Count];

int i = 0;
if (matches.Count > 0)
{
    foreach (Match m in matches)
    {
        allDiamonds[i] = String.Format("{0}", m);
        i++;
    }
}

Thank you for your help

Upvotes: 0

Views: 57

Answers (1)

vks
vks

Reputation: 67988

(<span\s*id="-?\d+"(?:(?!<\/span>).)*<\/span>)

Try this.See demo.

http://regex101.com/r/hQ1rP0/66

Upvotes: 2

Related Questions