C# 如何使用Regex.Replace修改匹配的字符串?
假设我要转换这些字符串: www.myexample.com和C# 如何使用Regex.Replace修改匹配的字符串?,c#,regex,C#,Regex,假设我要转换这些字符串: www.myexample.com和 进入: <a href='http://www.myexample.com'>http://www.myexample.com</a> 使用Regex.Replace 我想到了这个: Regex.Replace(string, pattern, "<a href=\"$&\">$&</a>") Regex.Replace(字符串,模式,”) 我的问题是,我不知
进入:
<a href='http://www.myexample.com'>http://www.myexample.com</a>
使用Regex.Replace
我想到了这个:
Regex.Replace(string, pattern, "<a href=\"$&\">$&</a>")
Regex.Replace(字符串,模式,”)
我的问题是,我不知道如何检查匹配的字符串$&是否以http://开头,并在必要时添加它
什么想法?
< P>如果你不必考虑<代码> http或诸如此类的东西,你也许可以用这个:Regex.Replace(string, @"(?:http://)?(.+)", "<a href=\"http://$1\">http://$1</a>")
Regex.Replace(字符串,@“(?:http:/)?(.+)”,“”)
< /代码> 如果你不必考虑<代码> http或诸如此类的东西,你也许可以用这个:
Regex.Replace(string, @"(?:http://)?(.+)", "<a href=\"http://$1\">http://$1</a>")
Regex.Replace(字符串,@“(?:http:/)?(.+)”,“”)
你使用的模式是什么?你使用的模式是什么?如果你想考虑<代码> http:/COD>可能性,那么将<代码> http:<代码>改为<代码> http::/c> >如果你想考虑<代码> http:/c> >可能性,然后将<代码> http:< /c> > <代码> http:://>