使用.NET框架,我试图用一个斜杠替换字符串中的双斜杠字符,但它似乎删除了一个额外的字符,我不知道为什么.
我有一个字符串:
http://localhost:4170/RCRSelfRegistration//Default.aspx
Run Code Online (Sandbox Code Playgroud)
我的正则表达式是:
[^(://|:\\\\)](\\\\|//|\\/|/\\)
Run Code Online (Sandbox Code Playgroud)
而返回值是:
http://localhost:4170/RCRSelfRegistratio/Default.aspx
Run Code Online (Sandbox Code Playgroud)
您可以看到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;
}
Run Code Online (Sandbox Code Playgroud)
[^(://|:\\\\)]
不像你想象的那样工作.
[]
是一个字符范围 - 它匹配范围中包含的单个字符.
[^:]
将匹配冒号以外的任何字符.这可能更接近你想要的.
你可能真正想要的是一个零宽度的lookbehind断言:(?<!:)