【问题标题】:Powershell Substring replacement [duplicate]Powershell子字符串替换[重复]
【发布时间】:2017-07-02 01:51:05
【问题描述】:

乡亲

我无法在 PowerShell 中替换以下字符串,最初我怀疑它是由于正则表达式,并使用 [regex]::Escape()\ 解析为双斜杠,但它仍然无法正常工作。可以推荐一下吗?

PS C:\User>"C:clog" -replace "C:c" , ""
PS C:\User>log
PS C:\User>"C:\\c\\log" -replace "C:\\c\\", ""
PS C:\User>C:\\c\\log

【问题讨论】:

  • 您在输入字符串中定义了 2 个反斜杠,因此模式中需要 4 个:"C:\\c\\log" -replace "C:\\\\c\\\\", ""
  • Wiktor 你是对的,我现在通过输入字符串上的另一个 [regex]::Escape() 实现了这一点。请在答案中发布。

标签: regex string powershell substring


【解决方案1】:

使用Split-Path,它就是为此而设计的:

Split-Path "C:\\c\\log" -Leaf

# returns "log"

【讨论】:

  • 不,它不仅仅是提取日志,我还有 C:\\c\\log\\loggin\\logo
  • 如果它更适合您的情况,您可以坚持使用正则表达式。
猜你喜欢
  • 1970-01-01
  • 2014-11-05
  • 2022-01-07
  • 2013-02-01
  • 1970-01-01
  • 2013-05-18
  • 1970-01-01
  • 2015-12-08
  • 2015-10-08
相关资源
最近更新 更多