c# - Remove port number in image url?


Keywords:c# 


Question: 

I try to get image url using regex, and my code is below.

string IMG_REX_PATTERN = @"<[Ii][Mm][Gg][^>]*src\s*=\s*[\""\']?(?<IMAGE_URL>[^""'>\s]*)[\""\']?[^>]*>"; 
Match match = Regex.Match(result[i]["N_Dext5EditorField"].ToString(), IMG_REX_PATTERN, RegexOptions.IgnoreCase); 
string src = string.Empty; 
  if (match.Length > 0) 
 { 
     //portalUrl = https://test.beta.co.kr
     string portalUrl = DevelopmentHelper.GetPortalUrl(); 
     src = match.Groups[1].Value.Replace(portalUrl, ""); 
} 
else 
{ 
    src = ""; 
}

But, sometimes src contains port number like this.

src = :443/dext5editordata/2017/12/20171228_191217524_37634.png

I want to get url like this(not included port number)

src = /dext5editordata/2017/12/20171228_191217524_37634.png

I thought easy way is portalUrl + ":443", but port number is sometimes diffrent.

How can I fix my code? Please help me.


2 Answers: 

  Just update it  

  string src = ":443/dext5editordata/2017/12/20171228_191217524_37634.png";
  int firstindex=src.IndexOf("/");
  string witoutportno=src.Substring(firstindex);

You can also Check my code here

 

based on your replies, I changed my code.

if (match.Length > 0) 
{ 
     //portalUrl = https://test.beta.co.kr
     string portalUrl = DevelopmentHelper.GetPortalUrl(); 
     string tempsrc = match.Groups[1].Value; 

     var uri = new UriBuilder(tempsrc);
     string targetUrl = uri.Uri.ToString();
     src = targetUrl .Replace(portalUrl, "");
} 
else 
{ 
    src = ""; 
}