I am trying to match the following string and replace it with the first capture 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.
Your error is that you have to put $1
in quotes: '$1'
. Otherwise it just replaces everything with the value of the variable $1
which is not set thus with nothing.
However, instead of replacing it with the first capture group just replace everything until and including the backslash with nothing:
$name -replace '^\w+\\'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.