Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex replace help

Tags:

.net

regex

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;
}
like image 276
Jeremy Avatar asked Sep 10 '26 14:09

Jeremy


2 Answers

[^(://|:\\\\)] doesn't work the way you think it does.

[] is a character range - it matches a single character that is contained in the range.

[^:] will match any character other than a colon. This might be closer to what you want.

What you probably really want is a zero-width lookbehind assertion: (?<!:)

like image 165
Douglas Leeder Avatar answered Sep 12 '26 05:09

Douglas Leeder


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.

like image 40
annakata Avatar answered Sep 12 '26 03:09

annakata