I am looking after fixing a bug and there I find this expression:-
directoryPath = Regex.Replace(directoryPath, "[^\\w\\.@-]", "");
but as a result of the expression all the high ascii characters in directory path are messed up, I am not good at regex and dont know about it but for 开发者_运维知识库now I have to fix the issue .
Can someone please explain me what this regular expression does?
It replaces anything that is NOT
word character
OR.
(dot) OR@
OR-
(dash)with nothing.
INPUT
var directoryPath = @"C.@-(:\abc123/\def.foo";
OUTPUT
C.@-abc123def.foo
Modified code to replace with space
and corresponding output
var directoryPath = @" @.-abcd efghi(^-^)/\:jklmNO@.-PQRSTUVW XYZ0123456789@.-";
Console.WriteLine(directoryPath);
//note change here
//second argument to Replace function is chanted from "" to " "
directoryPath = Regex.Replace(directoryPath, "[^\\w\\.@-]", " ");
Console.WriteLine(directoryPath);
Output:
@.-abcd efghi(^-^)/\:jklmNO@.-PQRSTUVW XYZ0123456789@.-
@.-abcd efghi - jklmNO@.-PQRSTUVW XYZ0123456789@.-
It seems that you are having encoding issues. For example, Regex
could have treated your string as ASCII when it really was stored as UTF-8.
It appears to be removing the first character from directoryPath
that is not a word character (digits, letters, underscores), period, @ symbol or hyphen.
I've just tried it with C:\Scratch\Filename.txt
, and it leaves me with CScratchFilename.txt
.
精彩评论