【问题标题】:Cut everything after dash in Powershell在 Powershell 中破折号后剪切所有内容
【发布时间】:2022-11-21 23:08:13
【问题描述】:

我得到的字符串数组看起来像这样:

Cola-12-0-15-300-122
Pepsi-123-34-543
7_Up-rrr-12-2342-2
Fanta_Mineral-1212-fgdfg-33

我需要从这些值中检索到破折号之前的第一个单词。

所以我会有

Cola
Pepsi
7_up
Fanta_Mineral

【问题讨论】:

    标签: powershell


    【解决方案1】:

    您可以使用 -replace 运算符删除第一个 --replace '-.*' 之后的所有内容

    $strings = -split @'
    Pepsi-123-34-543
    7_Up-rrr-12-2342-2
    Fanta_Mineral-1212-fgdfg-33
    '@
    
    $strings -replace '-.*'
    

    输出:

    Cola
    Pepsi
    7_Up
    Fanta_Mineral
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多