开发者

What does this regex expression do?

开发者 https://www.devze.com 2022-12-25 18:44 出处:网络
I am looking after fixing a bug and there I find this expression:- directoryPath = Regex.Replace(directoryPath, \"[^\\\\w\\\\.@-]\", \"\");

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

  1. word character OR

  2. . (dot) OR

  3. @ OR

  4. - (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.

0

精彩评论

暂无评论...
验证码 换一张
取 消