Author Posts

August 11, 2015 at 12:28 pm

Hello,

I need to extract the data from a file path that contains the recycle bin with the regular expression.

The path is: D:\$RECYCLE.BIN\SubFolder\Subfolder.

I tried the pattern below, but I cannot extract the path since the dollar sign follows immediately after the back slash character which requires an escape character \.

The code I have is:

$path = 'D:\$RECYCLE.BIN\SubFolder\Subfolder'
[regex]::match($path,"[C-Z]:\\\`$.*").value

I would expect the regex will return the data as variable $path, but it does not. However, if I use [regex]::match($path,"[C-Z]:\\.*").value, I got only D:\.BIN which does not include the whole string.

How can I set the pattern for the match so that I can get the data assigned to the variable $path?

Thank you in advance.

August 12, 2015 at 3:23 am

Below code works for me after removing the backtick and replacing the double quotes of the pattern with singles to prevent PowerShell variable expansion.


$path = 'D:\$RECYCLE.BIN\SubFolder\Subfolder'
[regex]::match($path,'[C-Z]:\\\$.*').value