在DownloadString中查找并替换href [英] find and replace href in DownloadString
本文介绍了在DownloadString中查找并替换href的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在使用webclient获取下载字符串后,我希望将所有的href部分替换为新的plz帮助
after use webclient to get downloadstring, i wish to replace all the href part with something new, plz help
推荐答案
String.Replace [
String.Replace[^] will help you replace string with other text.
使用 Regex.Replace [ ^ ]
试试这个:
Use Regex.Replace[^]
Try this:
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
// original string
string input = "<a href='http://oldurl.com'>somewhere</a>";
// use Regex.Replace to replace the pattern in the input.
string pattern = @"(?<=href=[']).+(?=')";
string output = Regex.Replace(input, pattern,"http://newurl.com");
// Write the output.
Console.WriteLine(input);
Console.WriteLine(output);
}
}
它将输出:
It will output:
<a href='http://oldurl.com'>somewhere</a>
<a href='http://newurl.com'>somewhere</a>
Learn more about Regex[^]
这篇关于在DownloadString中查找并替换href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文