Ranjeet
Ranjeet

Reputation: 173

What does this regex expression do?

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?

Upvotes: 0

Views: 235

Answers (3)

Pratik Deoghare
Pratik Deoghare

Reputation: 37172

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

    [email protected]

Modified code to replace with space and corresponding output

    var directoryPath = @"  @.-abcd efghi(^-^)/\:[email protected]   [email protected]";

    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(^-^)/\:[email protected]   [email protected]
  @.-abcd efghi  -     [email protected]   [email protected]

Upvotes: 3

Andy Shellam
Andy Shellam

Reputation: 15535

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.

Upvotes: 0

badp
badp

Reputation: 11813

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.

Upvotes: 1

Related Questions