在 Powershell 中使用正则表达式匹配 $$ [英] Matching $$ using Regex in Powershell

查看:80
本文介绍了在 Powershell 中使用正则表达式匹配 $$的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用正则表达式测试器:
<代码>(?\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?>userDataXP>[a-zA-Z 0-9]*\$\$)) 匹配 \\server\e$\users\user1$$

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$$

使用 PowerShell 我必须在用户文件路径中的第二个 $ 之前添加一个反引号以使其匹配:

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]*\\(?[a-zA-Z 0-9]*\$\$))"真的

否则失败:

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

以下脚本的输入文件包含用户名都包含 $$ 的文件路径.我希望正则表达式能够捕获 user$$,创建一个仍然带有 $$ 的目录,并将文件复制到它.如何在以下脚本中解决此问题?

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
    }


}

推荐答案

只需在定义 regex 和字符串的地方使用单引号,这样你就不需要使用反引号转义 $.

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]*\$\$))'

输出:

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]*\$\$))"

$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]*\$\$))'

这篇关于在 Powershell 中使用正则表达式匹配 $$的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆