Change the relative path of the captured image to the absolute path

Source: Internet
Author: User

 

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

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.