C#正则表达式URL替换

时间:2017-09-22 20:08:16

标签: regex

请你在c#中提供正则表达式?

我正在尝试将网址中的域名更改为一个域,并将文件带到路径

中的第一级

实施例

http://mysit1e.com/1.html 
http://mysit1e.com/jo.png

会变成:

network :
    host : 127.0.0.1  //or ifconfig (ex eth0 : IP)

1 个答案:

答案 0 :(得分:0)

String inputUrls  = "http://goo.com/working/1.html http:/boo.com/kit/jo.png";
String newBaseUrl = "http:/mysit1e.com/"
String rplReg     =@"http.*?(?=[^/\s]+?(\s+|$))"
String outUrls    = Regex.Replace(inputUrls,rplReg,newBaseUrl);

此输出应为

http:/mysit1e.com/1.html http:/mysit1e.com/jo.png

正则表达式解释

http     # Only works for http headers
.*?      # Rest of the base url
(?=      # Positive Lookahead(This is not selected for replacement) Only match the URL if it is followed by this condition
[^\s/]+? # Condition for the last leg shouldnt have `/` or a space `\s`
(\s+|$)  # last leg should be followed by either space `\s+` or end of string.
)        # End of Positive Lookahead

最后但并非最不要忘记using System.Text.RegularExpressions;