Question:
wanted one help, wanted a regex to eliminate a “\” and what ever come before it,
1 2 3 |
Input should be "vmvalidate\administrator" and the output should be just "administrator" |
Answer:
1 2 |
$result = $subject -creplace '^[^\\]*\\', '' |
removes any non-backslash characters at the start of the string, followed by a backslash:
Explanation:
1 2 3 4 |
^ # Start of string [^\\]* # Match zero or more non-backslash characters \\ # Match a backslash |
This means that if there is more than one backslash in the string, only the first one (and the text leading up to it) will be removed. If you want to remove everything until the last backslash, use
1 2 |
$result = $subject -creplace '(?s)^.*\\', '' |