Backslashes in Powershell

2019-06-17 22:48发布

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

2条回答
狗以群分
2楼-- · 2019-06-17 23:35

-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 is just taking whatever characters you supply to use as the delimiter to stick between the strings being joined.

查看更多
神经病院院长
3楼-- · 2019-06-17 23:42

-split is accepting a regular expression where backslash is special character, so it need to be escaped

查看更多
登录 后发表回答