PowerShell将字符串切成两半

时间:2019-03-12 11:43:59

标签: regex powershell substring backtracking backreference

我的线条由相同的两半组成,我想从其中去除一半;例如,'AbcAbc'应该变成'Abc'

数据始终看起来像:

10.22.20.106/tcp/8010.22.20.106/tcp/80
10.22.20.46/tcp/44310.22.20.46/tcp/443
10.22.20.90/tcp/44310.22.20.90/tcp/443
10.22.20.90/tcp/8010.22.20.90/tcp/80
10.22.20.89/tcp/44310.22.20.89/tcp/443
10.22.20.89/tcp/8010.22.20.89/tcp/80
10.22.20.29/tcp/44310.22.20.29/tcp/443
10.22.20.29/tcp/8010.22.20.29/tcp/80
10.22.20.122/tcp/44310.22.20.122/tcp/443
10.22.20.123/tcp/44310.22.20.123/tcp/443
10.22.20.79/tcp/44310.22.20.79/tcp/443
10.22.20.79/tcp/8010.22.20.79/tcp/80
10.22.20.78/tcp/44310.22.20.78/tcp/443
10.22.20.78/tcp/8010.22.20.78/tcp/80
10.22.20.74/tcp/44310.22.20.74/tcp/443
10.22.20.74/tcp/8010.22.20.74/tcp/80
10.22.20.22/tcp/44310.22.20.22/tcp/443
10.22.20.22/tcp/8010.22.20.22/tcp/80
10.22.20.99/tcp/44310.22.20.99/tcp/443
10.22.20.99/tcp/8010.22.20.99/tcp/80
10.22.20.54/tcp/44310.22.20.54/tcp/443
10.22.20.54/tcp/8010.22.20.54/tcp/80

我先计算字符串中的字符数,然后将其减半,但不确定如何使用计算出的(减半)字符数来剪切原始字符串。

$vip_ip = $vip_line.("Virtual IP Address/Protocol/Port")
$half_string = $vip_ip.length /2

$ vip_ip.length 44

$ half_string 22

$vip_cut = $vip_ip.(0,-$halfstring)

1 个答案:

答案 0 :(得分:2)

您正在尝试收集字符串方法"".SubString()

所需的信息

在您的情况下,您只需要这样的东西:

$vip_ip.substring(0,$halfstring)

从字符串的开始到一半。因为答案是相同的,所以也可以从一半开始到结尾。 SubString()支持这两种重载。

$vip_ip.substring($halfstring)