Haha, some paths are relative to those written when you capture links and images on a web page, such .. /and so on. I wrote a function to solve some problems, but not all of them. Which of the following experts is interested in providing some advice?
Code
# Change the relative path of the region A tag to the absolute path
Private static void AHrefFilter (ref string content, string Url, ref string MatchPattern)
{
// Change the relative path of tag A to the absolute path.
MatchPattern = @ "\ bhref = [^: ^; ^ # ^ + ^>] *?> ";
MatchCollection mcAhref = Regex. Matches (content, MatchPattern, RegexOptions. IgnoreCase );
Foreach (Match m in mcAhref)
{
String hrefStr = m. Value. Replace ("href", ""). Replace ("HREF", ""). Substring (1 );
String hrefStrTrim = hrefStr;
If (! HrefStr. Contains ("http") & hrefStr. StartsWith ("\"/"))
{
String urlagain = Regex. Match (Url, "http: // ([^/] *? /) "). Value. Trim ();
HrefStr = "\" "+ urlagain. Substring (0, urlagain. Length-1) + hrefStr. Substring (1 );
}
If (! HrefStr. Contains ("http") & hrefStr. Contains ("\"../../"))
{
HrefStr = hrefStr. Replace ("http://www.cnblogs.com /","");
HrefStr = "\" "+ Regex. Match (Url," http: // ([^/] *? /) {2} "). Value. Trim () + hrefStr. Substring (1 );
}
If (! HrefStr. Contains ("http") & hrefStr. Contains ("\"../"))
{
HrefStr = hrefStr. Replace ("../","");
HrefStr = "\" "+ Regex. Match (Url," http: // ([^/] *? /) {3} "). Value. Trim () + hrefStr. Substring (1 );
}
If (! HrefStr. Contains ("http ")&&! HrefStr. StartsWith ("\"/"))
{
HrefStr = "\" "+ Regex. Match (Url," http: // ([^/] *? /) {4} "). Value. Trim () + hrefStr. Substring (1 );
}
String newHrefStr = "href =" + hrefStr + "";
Content = content. Replace (m. Value, newHrefStr );
}
}
# Endregion