在 Powershell 中使用正则表达式匹配 $$ [英] Matching $$ using Regex in Powershell
问题描述
使用正则表达式测试器:
<代码>(?\\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]*\\(?
否则失败:
"\\server\e$\users\user1$$" -match "(?<UserDataPathXP>\\\\[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屋!