Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正则表达式:绝对url到相对url(C)_C#_Asp.net Mvc_Regex_Url - Fatal编程技术网

C# 正则表达式:绝对url到相对url(C)

C# 正则表达式:绝对url到相对url(C),c#,asp.net-mvc,regex,url,C#,Asp.net Mvc,Regex,Url,我需要一个正则表达式来运行下面这样的字符串,它将在某些条件下将绝对路径转换为相对路径 <p>This website is <strong>really great</strong> and people love it <img alt="" src="http://localhost:1379/Content/js/fckeditor/editor/images/smiley/msn/teeth_smile.gif" /></p>

我需要一个正则表达式来运行下面这样的字符串,它将在某些条件下将绝对路径转换为相对路径

<p>This website is <strong>really great</strong> and people love it <img alt="" src="http://localhost:1379/Content/js/fckeditor/editor/images/smiley/msn/teeth_smile.gif" /></p>
规则:

如果url包含/Content/I 要获取相对路径吗

如果url不包含 /Content/,它是一个外部文件, 绝对路径应该保持不变

不幸的是,Regex不是我的强项,在这一点上,这对我来说太高级了。如果有人能提供一些建议,我将不胜感激

提前谢谢

更新: 要回答评论中的问题:

应用正则表达式时,所有URL都将以http开头:// 这应该应用于img和a标记的src属性,而不是应用于标记之外的文本。
除非我没抓住重点,如果你替换

^(.*)([C|c]ontent.*)

你最终会得到

/Content/js/fckeditor/editor/images/smiley/msn/teeth_smile.gif
这只会发生在找到id内容时,因此在cae中,您有一个URL,例如:

http://localhost:1379/js/fckeditor/editor/images/smiley/msn/teeth_smile.gif
任何东西都不会被取代

希望能有帮助,我没有错过任何东西

更新

显然,考虑到您正在使用HTML解析器查找a href中的URL,如果您没有:-


干杯

除非我没抓住要点,如果你替换我的话

^(.*)([C|c]ontent.*)

你最终会得到

/Content/js/fckeditor/editor/images/smiley/msn/teeth_smile.gif
这只会发生在找到id内容时,因此在cae中,您有一个URL,例如:

http://localhost:1379/js/fckeditor/editor/images/smiley/msn/teeth_smile.gif
任何东西都不会被取代

希望能有帮助,我没有错过任何东西

更新

显然,考虑到您正在使用HTML解析器查找a href中的URL,如果您没有:-


为perl干杯,我不知道c:

s@(<(img|a)\s[^>]*?\s(src|href)=)(["'])http://[^'"]*?(/Content/[^'"]*?)\4@$1$4$5@g

如果c有类似于正则表达式的perl,那么移植就很容易了。

这就是perl,我不知道c:

s@(<(img|a)\s[^>]*?\s(src|href)=)(["'])http://[^'"]*?(/Content/[^'"]*?)\4@$1$4$5@g

如果C具有Perl类正则表达式,则很容易导出。

你应该考虑使用--你当前的算法依赖于外部文件,它们从不包含/内容/在它们的路径中,这对我来说似乎是危险的。MakeRelativeUri将决定一条相对路径是否可以从当前的Uri到SRC或HREF,而不管你或外部文件存储在道路上的变化。

你应该考虑使用--你当前的算法依赖于从不包含/内容/路径的外部文件,这对我来说似乎是危险的。MakeRelativeUri将确定是否可以创建从当前Uri到src或href的相对路径,而不考虑您或外部文件存储所做的更改。

此函数可以将HTML中的所有超链接和图像源转换为绝对URL,并且确保您也可以为CSS文件和Javascript修改它轻松归档:

Private Function ConvertALLrelativeLinksToAbsoluteUri(ByVal html As String, ByVal PageURL As String)
    Dim result As String = Nothing
    ' Getting all Href
    Dim opt As New RegexOptions
    Dim XpHref As New Regex("(href="".*?"")", RegexOptions.IgnoreCase)
    Dim i As Integer
    Dim NewSTR As String = html
    For i = 0 To XpHref.Matches(html).Count - 1
        Application.DoEvents()
        Dim Oldurl As String = Nothing
        Dim OldHREF As String = Nothing
        Dim MainURL As New Uri(PageURL)
        OldHREF = XpHref.Matches(html).Item(i).Value
        Oldurl = OldHREF.Replace("href=", "").Replace("HREF=", "").Replace("""", "")
        Dim NEWURL As New Uri(MainURL, Oldurl)
        Dim NewHREF As String = "href=""" & NEWURL.AbsoluteUri & """"
        NewSTR = NewSTR.Replace(OldHREF, NewHREF)
    Next
    html = NewSTR
    Dim XpSRC As New Regex("(src="".*?"")", RegexOptions.IgnoreCase)
    For i = 0 To XpSRC.Matches(html).Count - 1
        Application.DoEvents()
        Dim Oldurl As String = Nothing
        Dim OldHREF As String = Nothing
        Dim MainURL As New Uri(PageURL)
        OldHREF = XpSRC.Matches(html).Item(i).Value
        Oldurl = OldHREF.Replace("src=", "").Replace("src=", "").Replace("""", "")
        Dim NEWURL As New Uri(MainURL, Oldurl)
        Dim NewHREF As String = "src=""" & NEWURL.AbsoluteUri & """"
        NewSTR = NewSTR.Replace(OldHREF, NewHREF)
    Next
    Return NewSTR
End Function

此函数可以将HTML中的所有超链接和图像源转换为绝对URL,并且可以确保您也可以轻松修改CSS文件和Javascript文件:

Private Function ConvertALLrelativeLinksToAbsoluteUri(ByVal html As String, ByVal PageURL As String)
    Dim result As String = Nothing
    ' Getting all Href
    Dim opt As New RegexOptions
    Dim XpHref As New Regex("(href="".*?"")", RegexOptions.IgnoreCase)
    Dim i As Integer
    Dim NewSTR As String = html
    For i = 0 To XpHref.Matches(html).Count - 1
        Application.DoEvents()
        Dim Oldurl As String = Nothing
        Dim OldHREF As String = Nothing
        Dim MainURL As New Uri(PageURL)
        OldHREF = XpHref.Matches(html).Item(i).Value
        Oldurl = OldHREF.Replace("href=", "").Replace("HREF=", "").Replace("""", "")
        Dim NEWURL As New Uri(MainURL, Oldurl)
        Dim NewHREF As String = "href=""" & NEWURL.AbsoluteUri & """"
        NewSTR = NewSTR.Replace(OldHREF, NewHREF)
    Next
    html = NewSTR
    Dim XpSRC As New Regex("(src="".*?"")", RegexOptions.IgnoreCase)
    For i = 0 To XpSRC.Matches(html).Count - 1
        Application.DoEvents()
        Dim Oldurl As String = Nothing
        Dim OldHREF As String = Nothing
        Dim MainURL As New Uri(PageURL)
        OldHREF = XpSRC.Matches(html).Item(i).Value
        Oldurl = OldHREF.Replace("src=", "").Replace("src=", "").Replace("""", "")
        Dim NEWURL As New Uri(MainURL, Oldurl)
        Dim NewHREF As String = "src=""" & NEWURL.AbsoluteUri & """"
        NewSTR = NewSTR.Replace(OldHREF, NewHREF)
    Next
    Return NewSTR
End Function

问题:所有URL都是完全绝对的,也就是说,使用http://或ftp://或其他什么?您只想更改img标记的src属性吗?问题:所有URL都是完全绝对的,即,使用http://或ftp://或其他什么?您只想更改img标记的src属性?这肯定是更有效的方法。如果作者不使用HTML解析器,您可以编写更简单的内容?如果作者不使用HTML解析器,您可以编写更简单的内容?这个问题对您来说真的不是工作正则表达式。这个答案中的方法将提供更可靠的结果,并且更容易编写和理解引导。我感谢这里的建议。我不知道这种方法,完全同意正则表达式解决方案所涉及的风险。我会检查一下,很可能会接受这个答案。这个问题真的不是regex的工作。这个答案中的方法将提供更可靠的结果,并且更容易编写和理解引导。我感谢这里的建议。我不知道这种方法,完全同意正则表达式解决方案所涉及的风险。我会检查这一点,并很可能接受这个答案。