Thursday, July 16, 2009

Regular expression for HTML anchor Tag

hi,
I was in search of finding a regular expression that matches the anchor tag in HTML and updates the value of the href attributes.

i have gone through many of them but none supports the anchor tag in which the qoutes around href are not present i.e.

so here in the updated Regular expression that matches all kinds of anchor tag and then a small chunk of c# code to get and update the value of href attribute.


public string UpdateHTMLAnchorTag(string input)
{
string initialURL = PLACE THE ABOVE REGULAR EXPRESSION HERE
Regex regex = new Regex(initialURL, RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(input);
string ma = string.Empty;
foreach (Match m in matches)
{
ma = m.Result("${url}");
input = input.Replace(tempMa, "UpdatedMA");


}
return input;

}



1 comment:

gaurav said...

Really helpful. Really appreciate your efforts for posting this.

Thanks
Gaurav