我有以下字符串:
< \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"
我想使用正则表达式替换所有“NULL”实例,但我似乎无法获得正确的搜索模式。这是我的代码:
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++;
}
}
但是,这似乎并没有取代任何文本。正确的搜索模式是什么?
最佳答案
您的正则表达式的问题是 ^
字符,这意味着您的正则表达式只会查看字符串开头的 NULL
字符。把它取下来,你的代码就可以正常工作了。
关于C# - 如何使用正则表达式替换 NULL 字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41365455/