gpt4 book ai didi

powershell - 将字符串分成两个数组

转载 作者:行者123 更新时间:2023-12-02 23:07:52 25 4
gpt4 key购买 nike

我有一个像下面的字符串

abc-ijk-90-hortons-899-mnb-8037-POI-AS-QWE-ZXC-TYUIOP-KEW-197.127.255.0/23
abc-ijk-90-hortons-975-mnb-8024-POI-AS-QWE-ZXC-TYUIOP-KUYW-198.157.150.0/23
abc-ijk-90-hortons-356-mnb-8022-POI-AS-QWE-ZXC-TYUIOP-JHT-199.127.132.0/23

我想把字符串分成
c1= {8037,8024,8022}
c2= {POI-AS-QWE-ZXC-TYUIOP-KEW-197.127.255.0/23,POI-AS-QWE-ZXC-TYUIOP-KUYW-198.157.150.0/23,POI-AS-QWE-ZXC-TYUIOP-JHT-199.127.132.0/23}

c3应该显示如下
Number  Name
8037 POI-AS-QWE-ZXC-TYUIOP-KEW-197.127.255.0/23
8024 POI-AS-QWE-ZXC-TYUIOP-KUYW-198.157.150.0/23
8022 POI-AS-QWE-ZXC-TYUIOP-JHT-199.127.132.0/23

最佳答案

  • -处拆分字符串的行,但将拆分限制为8次,
    因此,将POI-...保持在一行的最后一行
  • 然后选择从零开始的索引[6,7]放入vars $ Number,$ Name
  • 构建这两个变量的[PSCustomObject]。
  • 最终从$ c3的属性中构建$ c1,$ c2

    $string = @"
    abc-ijk-90-hortons-899-mnb-8037-POI-AS-QWE-ZXC-TYUIOP-KEW-197.127.255.0/23
    abc-ijk-90-hortons-975-mnb-8024-POI-AS-QWE-ZXC-TYUIOP-KUYW-198.157.150.0/23
    abc-ijk-90-hortons-356-mnb-8022-POI-AS-QWE-ZXC-TYUIOP-JHT-199.127.132.0/23
    "@ -split '\r?\n'

    $c3 = foreach ($line in $string){
    $Number,$Name= ($line -split '-',8)[6,7]
    [PSCustomObject]@{
    Number = $Number
    Name = $Name
    }
    }
    # it's not quite clear what type you expect $c1,$c2 to be.
    # a simple joined string
    $c1 = '{'+($c3.Number -join ',')+'}'
    $c2 = '{'+($c3.Name -join ',')+'}'

    $c1;$c2;$c3

    样本输出:
    {8037,8024,8022}
    {POI-AS-QWE-ZXC-TYUIOP-KEW-197.127.255.0/23,POI-AS-QWE-ZXC-TYUIOP-KUYW-198.157.150.0/23,POI-AS-QWE-ZXC-TYUIOP-JHT-199.127.132.0/23}

    Number Name
    ------ ----
    8037 POI-AS-QWE-ZXC-TYUIOP-KEW-197.127.255.0/23
    8024 POI-AS-QWE-ZXC-TYUIOP-KUYW-198.157.150.0/23
    8022 POI-AS-QWE-ZXC-TYUIOP-JHT-199.127.132.0/23

  • 关于powershell - 将字符串分成两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55459274/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com