Regex replace help

Posted by Jeremy on Stack Overflow See other posts from Stack Overflow or by Jeremy
Published on 2009-01-27T17:34:22Z Indexed on 2010/03/08 15:51 UTC
Read the original article Hit count: 297

Filed under:
|

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;
}

© Stack Overflow or respective owner

Related posts about regex

Related posts about .NET