Powershell 中的反斜杠 [英] Backslashes in Powershell

查看:130
本文介绍了Powershell 中的反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 -split 参数的字符串需要两个反斜杠而 -join 参数的字符串只需要一个反斜杠?反引号是 Powershell 中的转义字符.字符前面的反斜杠有什么作用?

Why does the string for the -split parameter require two backslashes while the string for the -join parameter requires only one backslash? The backtick is the escape character in Powershell. What does a backslash preceding a character do for you?

$path = 'C:\folder\test\unit1\testing\results\report.txt'

$path -split '\\' -notlike '*test*' -join '\'

http://powershell.com/cs/blogs/tips/archive/2014/06/17/fun-with-path-names.aspx

推荐答案

-split 默认在正则表达式上拆分.所以正则表达式需要转义反斜杠.您可以告诉 PowerShell 不要像这样使用正则表达式:

-split splits on a regex by default. So it is the regex that requires escaping a backslash. You can tell PowerShell not to use a regex like so:

$path -split '\',-1,'SimpleMatch'

-join 只是将您提供的任何字符用作分隔符以粘贴在被连接的字符串之间.

-join is just taking whatever characters you supply to use as the delimiter to stick between the strings being joined.

这篇关于Powershell 中的反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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