正则替换问题请教

kellyhapyy 2009-12-17 10:12:53
我想把
<a href=\"http://www.aabb.net/cc/dddd/可变数字.html\">
替换成:
<a href=\"http://www.aabb.net/china-cc/dddd/e-可变数字.html\">
如何写正则.

在:CC前加上:china-
在“可变数字”前加了e-
...全文
71 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
silentwins 2009-12-17
  • 打赏
  • 举报
回复
string subjectString = "<a href=\"http://www.aabb.net/cc/dddd/1234.html\"> ";
string resultString = null;
try
{
subjectString = subjectString.Replace("cc","china-cc");
resultString = Regex.Replace(subjectString, @"(?<first>\d+)", @"e-${first}");
}
catch (ArgumentException ex)
{
// Syntax error in the regular expression
}
-过客- 2009-12-17
  • 打赏
  • 举报
回复
try...

string test = "<a href=\"http://www.aabb.net/cc/dddd/123.html\"> ";
Regex reg = new Regex(@"(?i)<a\s+href=""http://www.aabb.net/cc/dddd/(\d+).html"">");
string result = reg.Replace(test, "<a href=\"http://www.aabb.net/china-cc/dddd/e-$1.html\">");
Lovely_baby 2009-12-17
  • 打赏
  • 举报
回复

using System;
using System.Collections;
using System.Text.RegularExpressions;
public class MyClass
{
public static void Main()
{
string str_1="sHjhgsdKkjhLjk";
Console.WriteLine(str_1+"\n");
Console.WriteLine(Regex.Replace(str_1,"[A-Z]",new MatchEvaluator(Replace)));
//new MatchEvaluator(Replace)用委托,对匹配的字符处理
RL();
}
public static string Replace(Match m)//
{
return " "+m.Value;
}

#region Helper methods

private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}

private static void RL()
{
Console.ReadLine();
}

private static void Break()
{
System.Diagnostics.Debugger.Break();
}

#endregion
}

这是给指定字符前去空格~~


string A = "B0B0B0B0B0B0B0B0B";
string B = "B";
string C = "C";

Regex r = new Regex("[" + B + "]");

if (r.IsMatch(A))
{
A = r.Replace(A,C,3,0);
}

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧