【发布时间】:2009-01-27 17:34:22
【问题描述】:
使用 .NET 框架,我试图用单斜杠替换字符串中的双斜杠字符,但它似乎删除了一个额外的字符,我不知道为什么。
我有一个字符串:
http://localhost:4170/RCRSelfRegistration//Default.aspx
我的正则表达式是:
[^(://|:\\\\)](\\\\|//|\\/|/\\)
而返回值为:
http://localhost:4170/RCRSelfRegistratio/Default.aspx
可以看到 RCRSelfRegistration 中的 n 已经被移除了。我不知道为什么。
/// <summary>
/// Match on double slashes (//, \\, /\, \/) but do not match :// or :\\
/// </summary>
private const string strMATCH = @"[^(://|:\\\\)](\\\\|//|\\/|/\\)";
/// <summary>
/// Replace double slashes with single slash
/// </summary>
/// <param name="strUrl"></param>
/// <returns></returns>
public static string GetUrl(string strUrl)
{
string strNewUrl
System.Text.RegularExpressions.Regex rxReplace =
new System.Text.RegularExpressions.Regex(strMATCH);
strNewUrl = rxReplace.Replace(strUrl, "/");
return strNewUrl;
}
【问题讨论】: