Regular deletion that ever appears before "\" with powershell

he needed one help, he wanted the regular expression to remove the "\" and what ever was,

Input should be "vmvalidate\administrator" and the output should be just "administrator" 
+4
source share
3 answers
 $result = $subject -creplace '^[^\\]*\\', '' 

removes any characters without a backslash at the beginning of a line, and then a backslash:

Explanation:

 ^ # Start of string [^\\]* # Match zero or more non-backslash characters \\ # Match a backslash 

This means that if there is more than one backslash in a line, only the first (and the text preceding it) will be deleted. If you want to delete everything until the last backslash, use

 $result = $subject -creplace '(?s)^.*\\', '' 
+9
source

No need to use regex, try the split method:

 $string.Split('\')[-1] 
+8
source

So I did before I learned about regular expression or splitting.

 "vmvalidate\administrator".SubString("vmvalidate\administrator".IndexOf('\')+1) 
0
source

Source: https://habr.com/ru/post/1482187/


All Articles