正则表达式 替换最后一个匹配的项

shclhs 2008-10-27 01:45:09
这里我有一个地址,我想替换其中的一部分, 譬如:
替换前
d:\ttt1\cabc2\test3\tets4

替换后:
d:\ttt1\cabc2\NewContent\tets4


这个怎么写.

...全文
1469 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yijianxiangde100 2008-10-27
  • 打赏
  • 举报
回复
//问题的提出是:
///
//替换 最后一个 \ 和 倒数第二个 \ 之间的内容
//替换前:d:\ttt1\cabc2\test3\tets4
//替换后:
//d:\ttt1\cabc2\NewContent\tets4
问题的解决如下:

写了一个函数,用来处理次过程:
(1)用模式匹配找到反斜杠的位置,把它存储到Int 型的数组中
(2)找到倒数第一个与倒数第二个位置
(3)把这两个位置之间的字符拼接起来,构成字符串
(4)用Replace方法替代原来的字符串
/// <summary>
/// 字符串处理函数
/// </summary>
/// <param name="InputStr">替换前:d:\ttt1\cabc2\test3\tets4 </param>
/// <param name="ReplaceStr">要替换的字符串:NewContent</param>
/// <returns>替换后:d:\ttt1\cabc2\NewContent\tets4 </returns>
public static string ReplaceFunction(string InputStr,string ReplaceStr)
{
string OutputStr;
//har IndexChar = '\\';//查找的字符是 \反斜杠
string Pattern = @"\\S*\b";//查找的模式匹配是,以\反斜杠开始的字符串
MatchCollection Matches = Regex.Matches(InputStr, Pattern, RegexOptions.None);
//查找到位置,然后就是把位置存储到一个整形的数组中,然后就是最后两个位置拿出来,把中间的字符产截取了。用replace代替
int[] IndexArray=new int[Matches.Count];
int i = 0;
foreach (Match NextMatch in Matches)
{
IndexArray[i] = NextMatch.Index;
i++;
}
StringBuilder sbOld = new StringBuilder(500);

int strBegin = IndexArray[IndexArray.Length - 2]+1;
int strEnd = IndexArray[IndexArray.Length - 1]-1;

for (int j = strBegin; j <= strEnd; j++) //找到倒数第一个与倒数第二个index位置
{
sbOld.Append(InputStr[j]);
}
OutputStr = InputStr.Replace(sbOld.ToString(), ReplaceStr);
return OutputStr;
}
justindreams 2008-10-27
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zswang 的回复:]
C# codestring s = @"d:\ttt1\cabc2\test3\tets4";
Console.WriteLine(Regex.Replace(s, @"(.*?)\\([^\\]*)\\([^\\]*)$", "$1\\NewContent\\$3"));

string s1 = @"d:\ttt1\cabc2\test3\tets4\tets5";
Console.WriteLine(Regex.Replace(s1, @"(.*?)\\([^\\]*)\\([^\\]*)$", "$1\\NewContent\\$3"));





CSS coded:\ttt1\cabc2\NewContent\tets4
d:\ttt1\cabc2\test3\NewContent\tets5
[/Quote]

正解!!
王集鹄 2008-10-27
  • 打赏
  • 举报
回复 1
string s = @"d:\ttt1\cabc2\test3\tets4";
Console.WriteLine(Regex.Replace(s, @"(.*?)\\([^\\]*)\\([^\\]*)$", "$1\\NewContent\\$3"));

string s1 = @"d:\ttt1\cabc2\test3\tets4\tets5";
Console.WriteLine(Regex.Replace(s1, @"(.*?)\\([^\\]*)\\([^\\]*)$", "$1\\NewContent\\$3"));


d:\ttt1\cabc2\NewContent\tets4
d:\ttt1\cabc2\test3\NewContent\tets5
nattystyle 2008-10-27
  • 打赏
  • 举报
回复
那就split成数组,然后再拼起来就好了
7707 2008-10-27
  • 打赏
  • 举报
回复
string path = @"d:\ttt1\cabc2\test3\tets4";
string newPath = path.Substring(0, path.LastIndexOf("\\"));
newPath = newPath.Substring(0, newPath.LastIndexOf('\\') + 1);
newPath = newPath + "NewContent" + path.Substring(path.LastIndexOf('\\'));
Response.Write(newPath);
shclhs 2008-10-27
  • 打赏
  • 举报
回复
nattystyle 2008-10-27
  • 打赏
  • 举报
回复
倒数第二,是这个特征?
shclhs 2008-10-27
  • 打赏
  • 举报
回复
特征是

替换 最后一个 \ 和 倒数第二个 \ 之间的内容
shclhs 2008-10-27
  • 打赏
  • 举报
回复
说实话没有特征,因为是路径,所以没有明显特征
nattystyle 2008-10-27
  • 打赏
  • 举报
回复
你想替换的这个,有什么特征?
shclhs 2008-10-27
  • 打赏
  • 举报
回复
test3 并不是固定的阿
fangsp 2008-10-27
  • 打赏
  • 举报
回复
用StringReplace啊

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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