You can surely do this using regular expressions (it is a string manipulation afterall), but that may get a bit nasty, because HTML can be quite complicated. However, it is certainly a possible approach.
An alternative would be to parse the XHTML page into some structured hieararchy and then do the processing. The question is whether the pages are really valid XML. The XHTML specification requires that, but if you'll pick random page from the internet that claims to be XHTML, you may run into troubles.
- If no, then you need to parse them as HTML, which can be done using Html Agility Pack.
- If yes, then you can treat it as XML and use standard .NET classes to parse it.
The second case could be done using LINQ to XML like this:
var xs = from span in doc.Descendant("span")
let tag = span.Attribute("tag")
where tag != null && tag.Value == "x" select span;
forach(var x in xs) x.Value = "BAR!";
The obvious benefit is that this is much more readable and maintainable than a solution that would use regular expressions. Html Agility Pack provides a similar API (although I'm not familiar with it to write a sample).