Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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# 如何使用Regex.Replace修改匹配的字符串?_C#_Regex - Fatal编程技术网

C# 如何使用Regex.Replace修改匹配的字符串?

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(字符串,模式,”) 我的问题是,我不知

假设我要转换这些字符串:

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(字符串,模式,”)
我的问题是,我不知道如何检查匹配的字符串$&是否以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:://>