[英]PowerShell Regex replace produces empty output
I am trying to match the following string and replace it with the first capture goup: PCUNIT020\\Username; 我正在尝试匹配以下字符串并将其替换为第一个捕获goup:PCUNIT020 \\ Username; I need just the Username portion.
我只需要用户名部分。 I use the following regex:
我使用以下正则表达式:
$name="PCUNIT020\Username";
$regex="^\w+\\(.*)";
$newname=$name -replace $regex, $1;
$newname;
The shell does not output anything. shell不输出任何内容。
Your error is that you have to put $1
in quotes: '$1'
. 你的错误是,你必须把
$1
引号: '$1'
。 Otherwise it just replaces everything with the value of the variable $1
which is not set thus with nothing. 否则它只是用变量
$1
的值替换所有内容,因为没有设置任何内容。
However, instead of replacing it with the first capture group just replace everything until and including the backslash with nothing: 但是,不要用第一个捕获组替换它,而是直接替换所有内容,直到并包含反斜杠:
$name -replace '^\w+\\'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.