1

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.

1 Answer 1

3

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+\\'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.