Itchydon
Itchydon

Reputation: 2596

Matching $$ using Regex in Powershell

Using a Regex tester:
(?<UserDataPathXP>\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?>userDataXP>[a-zA-Z 0-9]*\$\$)) matches \\server\e$\users\user1$$

Using PowerShell I have to add a backtick before the second $ in the users filepath for it to match:

"\\server\e$\users\user1$`$" -match "(?<UserDataPathXP>\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userDataXP>[a-zA-Z 0-9]*\$\$))" True

Otherwise it fails:

"\\server\e$\users\user1$$" -match "(?<UserDataPathXP>\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userDataXP>[a-zA-Z 0-9]*\$\$))" False

The input file of the following script contains file paths with usernames all containing a $$. I want the regex to be able to capture the user$$, create a directory still with a $$, and copy files to it. How can I get around this problem in the following script?

$paths = get-content "E:\paths.txt" 
$ArchiveLocation = "\\Server1\Archive" 
$LogsData = Test-Path "$ArchiveLocation\Archived_UserData\Logs"
$LogsProfiles = Test-Path "$ArchiveLocation\Archived_UserProfiles\Logs"

$regxUsrProfPthXP = "(?<UserProfilePathXP>^((?![a-zA-Z 0-9]*\$\$).)*\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userProfileXP>[a-zA-Z 0-9]*\$))"
$regxUsrDataPthXP = "(?<UserDataPathXP>\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userDataXP>[a-zA-Z 0-9]*\$\$))"
if (-not $LogsProfiles) {
    md "$ArchiveLocation\Archived_UserProfiles\Logs"
}

if (-not $LogsData) {
    md "$ArchiveLocation\Archived_UserData\Logs"
}

foreach ($path in $paths) {

    if ($path -match $regxUsrProfPthXP) {
        $path = $matches.UserProfilePathXP
        $user = $matches.userProfileXP
        RoboCopy $path "$ArchiveLocation\Archived_UserProfiles\$user" /MIR /B /COPY:DATSOU /MOVE /LOG+:$ArchiveLocation\Archived_UserProfiles\Logs\$user.log /R:1 /W:1
    }

    if ($path -match $regxUsrDataPthXP) {
        $path = $matches.UserDataPathXP
        $user = $matches.userDataXP
        RoboCopy $path "$ArchiveLocation\Archived_UserData\$user" /MIR /B /COPY:DATSOU /MOVE /LOG+:$ArchiveLocation\Archived_UserData\Logs\$user.log /R:1 /W:1
    }


}

Upvotes: 4

Views: 275

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 58981

Just use single quotes where you define your regex and the string so you don't need to escape the $ using backticks.

'\\server\e$\users\user1$$' -match '(?<UserDataPathXP>\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userDataXP>[a-zA-Z 0-9]*\$\$))'

Output:

True

So all you have to do is to change

$regxUsrProfPthXP = "(?<UserProfilePathXP>^((?![a-zA-Z 0-9]*\$\$).)*\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userProfileXP>[a-zA-Z 0-9]*\$))"
$regxUsrDataPthXP = "(?<UserDataPathXP>\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userDataXP>[a-zA-Z 0-9]*\$\$))"

to

$regxUsrProfPthXP = '(?<UserProfilePathXP>^((?![a-zA-Z 0-9]*\$\$).)*\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userProfileXP>[a-zA-Z 0-9]*\$))'
$regxUsrDataPthXP = '(?<UserDataPathXP>\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userDataXP>[a-zA-Z 0-9]*\$\$))'

Upvotes: 4

Related Questions