Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - How to use Regex to replace NULL character?

Tags:

c#

regex

null

hex

I have the following string:

< \0\"\0E\0x\0t\0e\0n\0s\0i\0b\0i\0l\0i\0t\0y\0,\0v\0e\0r\0s\0i\0o\0n\0=\0\\\0\"\07\0.\00\0.\03\03\00\00\0.\00\0\\\0\"\0,\0p\0u\0b\0l\0i\0c\0K\0e\0y\0T\0o\0k\0e\0n\0=\0\\\0\"\0B\00\03\0F\05\0F\07\0F\01\01\0D\05\00\0A\03\0A\0\\\0\"\0,\0f\0i\0l\0e\0V\0e\0r\0s\0i\0o\0n\0=\0\\\0\"\07\0.\00\0.\09\04\06\06\0.\01\0\\\0\"\0,\0c\0u\0l\0t\0u\0r\0e\0=\0\\\0\"\0n\0e\0u\0t\0r\0a\0l\0\\\0\"\0\"\0=\0h\0e\0x\0(\07\0)\0:\07\08\0,\0\\\0"

In notepad++ it looks something like: enter image description here

I would like to replace all "NULL" instances using Regex, but I can't seem to get the correct search pattern. This is my code:

        FileInfo file = new FileInfo(path);
        string line;
        using (StreamReader reader = new StreamReader(file.FullName))
        {
            while ((line = reader.ReadLine()) != null)
            {
                Regex rgx = new Regex(@"^[\00|\0]");
                line = rgx.Replace(line, "");

                System.Console.WriteLine(line);
                CurrentLine++;
            }
        }

However, this does not appear to be replacing any text. What would the correct search pattern for this be?

like image 628
Hooplator15 Avatar asked Dec 19 '25 13:12

Hooplator15


1 Answers

The problem with your regex is the ^ character which means that your regex will only look at the start of the string for the NULL character. Take it off and your code will work just fine.

like image 95
Nasreddine Avatar answered Dec 21 '25 04:12

Nasreddine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!