正则表达式删除“\"前面的内容使用 PowerShell [英] Regex to remove what ever comes in front of "\" using powershell

查看:139
本文介绍了正则表达式删除“\"前面的内容使用 PowerShell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要一个帮助,想要一个正则表达式来消除\"以及它之前的任何东西,

wanted one help, wanted a regex to eliminate a "\" and what ever come before it,

Input should be "vmvalidate\administrator" 
and the output should be just "administrator"

推荐答案

$result = $subject -creplace '^[^\\]*\\', ''

删除字符串开头的所有非反斜杠字符,后跟反斜杠:

removes any non-backslash characters at the start of the string, followed by a backslash:

说明:

^      # 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

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

这篇关于正则表达式删除“\"前面的内容使用 PowerShell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆