删除图片网址中的端口号?

时间:2018-01-02 06:51:57

标签: c# regex split

我尝试使用正则表达式获取图片网址,我的代码在下面。

<div class="box">
	<a class="button" href="#popup1">Open Details</a>
</div>



<div id="popup1" class="overlay">  
<div class="popup">
      <div class="modal-dialog">  
           <div class="modal-content">  
                <div class="modal-header">  
                    
                     
                     <h4 class="modal-title">PHP Ajax Update MySQL Data Through Bootstrap Modal</h4>  
                </div>  
                <div class="modal-body">  
                     <form method="post" id="insert_form">  
                          <label>Enter Employee Name</label>  
                          <input type="text" name="name" id="name" class="form-control" />  
                          <br />  
                          <label>Enter Employee Address</label>  
                          <textarea name="address" id="address" class="form-control"></textarea>  
                          <br />  
                          <label>Select Gender</label>  
                          <select name="gender" id="gender" class="form-control">  
                               <option value="Male">Male</option>  
                               <option value="Female">Female</option>  
                          </select>  
                          <br />  
                          <label>Enter Designation</label>  
                          <input type="text" name="designation" id="designation" class="form-control" />  
                          <br />  
                          <label>Enter Age</label>  
                          <input type="text" name="age" id="age" class="form-control" />  
                          <br />  
                          <input type="hidden" name="employee_id" id="employee_id" />  
                          <input type="submit" name="insert" id="insert" value="Insert" class="btn btn-success" />  
                     </form>  
                </div>  
                <div class="modal-footer">  
                    
                     <a class="close btn btn-default" href="#">x</a>
                </div>  
           </div>  
      </div> 
      </div>
 </div>

但是,有时src包含这样的端口号。

SELECT a.* 
FROM tbl_makanan a
LEFT JOIN (
    SELECT id
    FROM tbl_makanan
    ORDER BY id ASC 
    LIMIT 1
) b USING (id)
WHERE b.id IS NULL

我想得到这样的网址(不包括端口号)

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 = ""; 
}

我认为简单的方法是portalUrl +&#34;:443&#34;,但端口号有时不同。

如何修复我的代码?请帮帮我。

2 个答案:

答案 0 :(得分:1)

  Just update it  

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

您也可以在此处查看我的代码

http://rextester.com/CRS51521

答案 1 :(得分:0)

根据您的回复,我更改了我的代码。

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 = ""; 
}