Apr 232009
 

I needed to do a replace using System.RegularExpressions.Regex.Replace() but was finding that if I passed the replacement string from a form, it would not correctly account for the Hexadecimal values I specified correctly.  I noticed Expresso was having this same issue shown below:

image

Instead of replacing \r\n (Carriage Return Line Feed) with a “,” (Hex 2C), it was replacing it with the literal.  I noticed this only happened when you passed the replace string in through a form; it would not happen if you hard coded Regex.Replace(“\r\n”,”\x2C”) into your .NET code.

I had to create a method that cleaned up all HEX replace strings from a form.  If you have a better way of doing this, please comment.

public string CleanUpRegexReplaceStringFromForm(string str_DataFromForm) { // \n The newline character. (ASCII 10) str_DataFromForm = str_DataFromForm.Replace("\\n", "\n"); // \r The carriage return character. (ASCII 13) str_DataFromForm = str_DataFromForm.Replace("\\r", "\r"); // \t The tab character. (ASCII 9) str_DataFromForm = str_DataFromForm.Replace("\\t", "\t"); // \x2C The , character. str_DataFromForm = str_DataFromForm.Replace("\\t", "\t").Replace("\\x00","\x00"); // \x All other ASCII Characters str_DataFromForm = str_DataFromForm.Replace("\\x00","\x00"); str_DataFromForm = str_DataFromForm.Replace("\\x01","\x01"); str_DataFromForm = str_DataFromForm.Replace("\\x02","\x02"); str_DataFromForm = str_DataFromForm.Replace("\\x03","\x03"); str_DataFromForm = str_DataFromForm.Replace("\\x04","\x04"); str_DataFromForm = str_DataFromForm.Replace("\\x05","\x05"); str_DataFromForm = str_DataFromForm.Replace("\\x06","\x06"); str_DataFromForm = str_DataFromForm.Replace("\\x07","\x07"); str_DataFromForm = str_DataFromForm.Replace("\\x08","\x08"); str_DataFromForm = str_DataFromForm.Replace("\\x09","\x09"); str_DataFromForm = str_DataFromForm.Replace("\\x0A","\x0A"); str_DataFromForm = str_DataFromForm.Replace("\\x0B","\x0B"); str_DataFromForm = str_DataFromForm.Replace("\\x0C","\x0C"); str_DataFromForm = str_DataFromForm.Replace("\\x0D","\x0D"); str_DataFromForm = str_DataFromForm.Replace("\\x0E","\x0E"); str_DataFromForm = str_DataFromForm.Replace("\\x0F","\x0F"); str_DataFromForm = str_DataFromForm.Replace("\\x10","\x10"); str_DataFromForm = str_DataFromForm.Replace("\\x11","\x11"); str_DataFromForm = str_DataFromForm.Replace("\\x12","\x12"); str_DataFromForm = str_DataFromForm.Replace("\\x13","\x13"); str_DataFromForm = str_DataFromForm.Replace("\\x14","\x14"); str_DataFromForm = str_DataFromForm.Replace("\\x15","\x15"); str_DataFromForm = str_DataFromForm.Replace("\\x16","\x16"); str_DataFromForm = str_DataFromForm.Replace("\\x17","\x17"); str_DataFromForm = str_DataFromForm.Replace("\\x18","\x18"); str_DataFromForm = str_DataFromForm.Replace("\\x19","\x19"); str_DataFromForm = str_DataFromForm.Replace("\\x1A","\x1A"); str_DataFromForm = str_DataFromForm.Replace("\\x1B","\x1B"); str_DataFromForm = str_DataFromForm.Replace("\\x1C","\x1C"); str_DataFromForm = str_DataFromForm.Replace("\\x1D","\x1D"); str_DataFromForm = str_DataFromForm.Replace("\\x1E","\x1E"); str_DataFromForm = str_DataFromForm.Replace("\\x1F","\x1F"); str_DataFromForm = str_DataFromForm.Replace("\\x20","\x20"); str_DataFromForm = str_DataFromForm.Replace("\\x21","\x21"); str_DataFromForm = str_DataFromForm.Replace("\\x22","\x22"); str_DataFromForm = str_DataFromForm.Replace("\\x23","\x23"); str_DataFromForm = str_DataFromForm.Replace("\\x24","\x24"); str_DataFromForm = str_DataFromForm.Replace("\\x25","\x25"); str_DataFromForm = str_DataFromForm.Replace("\\x26","\x26"); str_DataFromForm = str_DataFromForm.Replace("\\x27","\x27"); str_DataFromForm = str_DataFromForm.Replace("\\x28","\x28"); str_DataFromForm = str_DataFromForm.Replace("\\x29","\x29"); str_DataFromForm = str_DataFromForm.Replace("\\x2A","\x2A"); str_DataFromForm = str_DataFromForm.Replace("\\x2B","\x2B"); str_DataFromForm = str_DataFromForm.Replace("\\x2C","\x2C"); str_DataFromForm = str_DataFromForm.Replace("\\x2D","\x2D"); str_DataFromForm = str_DataFromForm.Replace("\\x2E","\x2E"); str_DataFromForm = str_DataFromForm.Replace("\\x2F","\x2F"); str_DataFromForm = str_DataFromForm.Replace("\\x30","\x30"); str_DataFromForm = str_DataFromForm.Replace("\\x31","\x31"); str_DataFromForm = str_DataFromForm.Replace("\\x32","\x32"); str_DataFromForm = str_DataFromForm.Replace("\\x33","\x33"); str_DataFromForm = str_DataFromForm.Replace("\\x34","\x34"); str_DataFromForm = str_DataFromForm.Replace("\\x35","\x35"); str_DataFromForm = str_DataFromForm.Replace("\\x36","\x36"); str_DataFromForm = str_DataFromForm.Replace("\\x37","\x37"); str_DataFromForm = str_DataFromForm.Replace("\\x38","\x38"); str_DataFromForm = str_DataFromForm.Replace("\\x39","\x39"); str_DataFromForm = str_DataFromForm.Replace("\\x3A","\x3A"); str_DataFromForm = str_DataFromForm.Replace("\\x3B","\x3B"); str_DataFromForm = str_DataFromForm.Replace("\\x3C","\x3C"); str_DataFromForm = str_DataFromForm.Replace("\\x3D","\x3D"); str_DataFromForm = str_DataFromForm.Replace("\\x3E","\x3E"); str_DataFromForm = str_DataFromForm.Replace("\\x3F","\x3F"); str_DataFromForm = str_DataFromForm.Replace("\\x40","\x40"); str_DataFromForm = str_DataFromForm.Replace("\\x41","\x41"); str_DataFromForm = str_DataFromForm.Replace("\\x42","\x42"); str_DataFromForm = str_DataFromForm.Replace("\\x43","\x43"); str_DataFromForm = str_DataFromForm.Replace("\\x44","\x44"); str_DataFromForm = str_DataFromForm.Replace("\\x45","\x45"); str_DataFromForm = str_DataFromForm.Replace("\\x46","\x46"); str_DataFromForm = str_DataFromForm.Replace("\\x47","\x47"); str_DataFromForm = str_DataFromForm.Replace("\\x48","\x48"); str_DataFromForm = str_DataFromForm.Replace("\\x49","\x49"); str_DataFromForm = str_DataFromForm.Replace("\\x4A","\x4A"); str_DataFromForm = str_DataFromForm.Replace("\\x4B","\x4B"); str_DataFromForm = str_DataFromForm.Replace("\\x4C","\x4C"); str_DataFromForm = str_DataFromForm.Replace("\\x4D","\x4D"); str_DataFromForm = str_DataFromForm.Replace("\\x4E","\x4E"); str_DataFromForm = str_DataFromForm.Replace("\\x4F","\x4F"); str_DataFromForm = str_DataFromForm.Replace("\\x50","\x50"); str_DataFromForm = str_DataFromForm.Replace("\\x51","\x51"); str_DataFromForm = str_DataFromForm.Replace("\\x52","\x52"); str_DataFromForm = str_DataFromForm.Replace("\\x53","\x53"); str_DataFromForm = str_DataFromForm.Replace("\\x54","\x54"); str_DataFromForm = str_DataFromForm.Replace("\\x55","\x55"); str_DataFromForm = str_DataFromForm.Replace("\\x56","\x56"); str_DataFromForm = str_DataFromForm.Replace("\\x57","\x57"); str_DataFromForm = str_DataFromForm.Replace("\\x58","\x58"); str_DataFromForm = str_DataFromForm.Replace("\\x59","\x59"); str_DataFromForm = str_DataFromForm.Replace("\\x5A","\x5A"); str_DataFromForm = str_DataFromForm.Replace("\\x5B","\x5B"); str_DataFromForm = str_DataFromForm.Replace("\\x5C","\x5C"); str_DataFromForm = str_DataFromForm.Replace("\\x5D","\x5D"); str_DataFromForm = str_DataFromForm.Replace("\\x5E","\x5E"); str_DataFromForm = str_DataFromForm.Replace("\\x5F","\x5F"); str_DataFromForm = str_DataFromForm.Replace("\\x60","\x60"); str_DataFromForm = str_DataFromForm.Replace("\\x61","\x61"); str_DataFromForm = str_DataFromForm.Replace("\\x62","\x62"); str_DataFromForm = str_DataFromForm.Replace("\\x63","\x63"); str_DataFromForm = str_DataFromForm.Replace("\\x64","\x64"); str_DataFromForm = str_DataFromForm.Replace("\\x65","\x65"); str_DataFromForm = str_DataFromForm.Replace("\\x66","\x66"); str_DataFromForm = str_DataFromForm.Replace("\\x67","\x67"); str_DataFromForm = str_DataFromForm.Replace("\\x68","\x68"); str_DataFromForm = str_DataFromForm.Replace("\\x69","\x69"); str_DataFromForm = str_DataFromForm.Replace("\\x6A","\x6A"); str_DataFromForm = str_DataFromForm.Replace("\\x6B","\x6B"); str_DataFromForm = str_DataFromForm.Replace("\\x6C","\x6C"); str_DataFromForm = str_DataFromForm.Replace("\\x6D","\x6D"); str_DataFromForm = str_DataFromForm.Replace("\\x6E","\x6E"); str_DataFromForm = str_DataFromForm.Replace("\\x6F","\x6F"); str_DataFromForm = str_DataFromForm.Replace("\\x70","\x70"); str_DataFromForm = str_DataFromForm.Replace("\\x71","\x71"); str_DataFromForm = str_DataFromForm.Replace("\\x72","\x72"); str_DataFromForm = str_DataFromForm.Replace("\\x73","\x73"); str_DataFromForm = str_DataFromForm.Replace("\\x74","\x74"); str_DataFromForm = str_DataFromForm.Replace("\\x75","\x75"); str_DataFromForm = str_DataFromForm.Replace("\\x76","\x76"); str_DataFromForm = str_DataFromForm.Replace("\\x77","\x77"); str_DataFromForm = str_DataFromForm.Replace("\\x78","\x78"); str_DataFromForm = str_DataFromForm.Replace("\\x79","\x79"); str_DataFromForm = str_DataFromForm.Replace("\\x7A","\x7A"); str_DataFromForm = str_DataFromForm.Replace("\\x7B","\x7B"); str_DataFromForm = str_DataFromForm.Replace("\\x7C","\x7C"); str_DataFromForm = str_DataFromForm.Replace("\\x7D","\x7D"); str_DataFromForm = str_DataFromForm.Replace("\\x7E","\x7E"); str_DataFromForm = str_DataFromForm.Replace("\\x7F", "\x7F"); return str_DataFromForm; }

  • K

    Ummmmm….. how about string.Replace(@”\\”, @”\”);

    Are you kidding?

  • Have you tried using it using your logic in a windows form?
    Try it and let us all know how it goes. I normally don’t post entries unless I really have found a viable solution.