C#替换多个href值

C#替换多个href值,c#,html,regex,html-parsing,C#,Html,Regex,Html Parsing,我有一块html,看起来像这样 <p><a href="docs/123.pdf">33</a></p> 基本上有数百个锚链接,我需要根据锚文本替换href。例如,我需要用以下内容替换上面的链接: <a href="33.html">33</a>. 。 我需要取值33,并在我的数据库上进行查找,以找到新的链接来替换href 我需要保持在原来的html所有如上所述 我该怎么做?救命啊 使用regexp查找值并替换

我有一块html,看起来像这样

<p><a href="docs/123.pdf">33</a></p>

基本上有数百个锚链接,我需要根据锚文本替换href。例如,我需要用以下内容替换上面的链接:

<a href="33.html">33</a>. 
我需要取值33,并在我的数据库上进行查找,以找到新的链接来替换href

我需要保持在原来的html所有如上所述


我该怎么做?救命啊

使用regexp查找值并替换
类似于
“/的regexp使用regexp查找值并替换

类似于
“/的regexp虽然这并不能回答您的问题,但HTML Agility Pack是处理和使用HTML的绝佳工具:

它至少可以让您更容易地获取所需的值并进行替换


包含使用HTML Agility Pack的链接:

尽管这并不能回答您的问题,但HTML Agility Pack是一个处理和使用HTML的好工具:

它至少可以让您更容易地获取所需的值并进行替换


包含使用HTML敏捷包的链接:

考虑使用以下粗略算法

using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

static class Program
{
  static void Main ()
  {
    string html = "<p><a href=\"docs/123.pdf\">33</a></p>"; // read the whole html file into this string.
    StringBuilder newHtml = new StringBuilder (html);
    Regex r = new Regex (@"\<a href=\""([^\""]+)\"">([^<]+)"); // 1st capture for the replacement and 2nd for the find
    foreach (var match in r.Matches(html).Cast<Match>().OrderByDescending(m => m.Index))
    {
       string text = match.Groups[2].Value;
       string newHref = DBTranslate (text);
       newHtml.Remove (match.Groups[1].Index, match.Groups[1].Length);
       newHtml.Insert (match.Groups[1].Index, newHref);
    }

    Console.WriteLine (newHtml);
  }

  static string DBTranslate(string s)
  {
    return "junk_" + s;
  }
}
使用系统;
使用System.Linq;
使用系统文本;
使用System.Text.RegularExpressions;
静态类程序
{
静态空隙干管()
{
string html=“

”;//将整个html文件读入该字符串。 StringBuilder newHtml=新的StringBuilder(html);
Regex r=newregex(@“\([^考虑使用以下粗略算法

using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

static class Program
{
  static void Main ()
  {
    string html = "<p><a href=\"docs/123.pdf\">33</a></p>"; // read the whole html file into this string.
    StringBuilder newHtml = new StringBuilder (html);
    Regex r = new Regex (@"\<a href=\""([^\""]+)\"">([^<]+)"); // 1st capture for the replacement and 2nd for the find
    foreach (var match in r.Matches(html).Cast<Match>().OrderByDescending(m => m.Index))
    {
       string text = match.Groups[2].Value;
       string newHref = DBTranslate (text);
       newHtml.Remove (match.Groups[1].Index, match.Groups[1].Length);
       newHtml.Insert (match.Groups[1].Index, newHref);
    }

    Console.WriteLine (newHtml);
  }

  static string DBTranslate(string s)
  {
    return "junk_" + s;
  }
}
使用系统;
使用System.Linq;
使用系统文本;
使用System.Text.RegularExpressions;
静态类程序
{
静态空隙干管()
{
string html=“

”;//将整个html文件读入该字符串。 StringBuilder newHtml=新的StringBuilder(html);
Regex r=new Regex(@“\([^将HTML转换成一个XmlDocument(您的标记是有效的,不是吗?),然后使用XPath查找所有带有
href
属性的
标记。应用转换并将新值分配给
href
属性。然后写出XmlDocument


简单!

将HTML转换成XML文档(您的标记是有效的,不是吗?),然后使用XPath查找所有带有
href
属性的
标记。应用转换并将新值分配给
href
属性。然后写出XML文档


易!

,你要做的是根据匹配的内容生成替换字符串。考虑使用<代码>正则表达式之一。
static void Main()
{
  Regex r = new Regex(@"<a href=""[^""]+"">([^<]+)");

  string s0 = @"<p><a href=""docs/123.pdf"">33</a></p>";
  string s1 = r.Replace(s0, m => GetNewLink(m));

  Console.WriteLine(s1);
}

static string GetNewLink(Match m)
{
  return string.Format(@"(<a href=""{0}.html"">{0}", m.Groups[1]);
}
static void Main()
{
正则表达式r=新正则表达式(@“

”; 字符串s1=r.Replace(s0,m=>GetNewLink(m)); 控制台写入线(s1); } 静态字符串GetNewLink(匹配m) {
返回字符串。格式化(@)(而不是显式创建委托方法。

< p>),您要做的是根据匹配的内容生成替换字符串。考虑使用<代码>正则表达式。替换“< /代码>”重载,采取一个例子:

static void Main()
{
  Regex r = new Regex(@"<a href=""[^""]+"">([^<]+)");

  string s0 = @"<p><a href=""docs/123.pdf"">33</a></p>";
  string s1 = r.Replace(s0, m => GetNewLink(m));

  Console.WriteLine(s1);
}

static string GetNewLink(Match m)
{
  return string.Format(@"(<a href=""{0}.html"">{0}", m.Groups[1]);
}
static void Main()
{
正则表达式r=新正则表达式(@“

”; 字符串s1=r.Replace(s0,m=>GetNewLink(m)); 控制台写入线(s1); } 静态字符串GetNewLink(匹配m) {
return string.Format(@)(而不是显式创建委托方法。

已更新,以便您可以看到html:-)您是否有html或有效的XML?您是动态生成此html(Web服务器)还是只想使用命令行或windows可执行文件一次/定期生成此文件?此外,您是否需要“替换”它们存在于现有文档中,或者您可以重新生成整个文档吗?更新后您可以看到html:-)您是否有html或有效的XML?您是动态生成此html(Web服务器)还是只想使用命令行或windows可执行文件一次/定期生成此文件?此外,您是否需要“替换”它们存在于现有文档中,或者您可以重新生成整个文档吗?我不能只是替换它们,我需要从锚文本中提取33并执行查找和替换URL。您可以使用捕获组捕获ancor文本,考虑到问题已经有
regex
标记,此答案尽可能有用。i不能只是替换它们,我需要从锚文本中提取33并执行查找并替换URL。您可以使用捕获组捕获ancor文本,考虑到该问题已经有了
regex
标记。这个答案非常有用,因为它不存在。我已经非常成功地使用了agility pack。regula的问题r表达式是,如果标记格式不正确,则可能会出现未命中或错误命中。HTML敏捷包正是OP需要的。我已经非常成功地使用了敏捷包。正则表达式的问题是,如果标记格式不正确,则可能会出现未命中或错误命中。HTML敏捷包正是这不是OP需要的。