Using the .NET framework, I’m trying to replace double slash characters in a string with a single slash, but it seems to be removing an extra character and I don’t know why.
I have a string:
http://localhost:4170/RCRSelfRegistration//Default.aspx
My regex is:
[^(://|:\\\\)](\\\\|//|\\/|/\\)
And the return value is:
http://localhost:4170/RCRSelfRegistratio/Default.aspx
You can see that the n in RCRSelfRegistration has been removed. I am not sure why.
/// <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; }
The first part of your regex ‘[^(://|:\\)]’ matches any character which is not ‘(:/|\’ (as tomalak points out, the negset matches all the characters within it, with no futher processing logic), which includes the ‘n’ immediately before ‘//default.aspx’ – it’s not a zero-width assertion.
What you probably want to do is change that part of the pattern to a zero-width lookbehind to make sure the slash character is not preceded by a colon.