在powershell中剪切url的一部分
我有一些URL需要剪切和分离每个URL的第一部分,即在powershell中剪切url的一部分,powershell,Powershell,我有一些URL需要剪切和分离每个URL的第一部分,即example1.com,example2.com,example3.com,从每一行开始并存储在一个变量中 url.csv中的内容 https://example1.com/v1/test/f3de-a8c6-464f-8166-9fd4 https://example2.com/v1/test/14nf-d7jc-54lf-fd90-fds8 https://example3.com/v1/test/bd38-17gd-2h65-0j3b-
example1.com
,example2.com
,example3.com
,从每一行开始并存储在一个变量中
url.csv中的内容
https://example1.com/v1/test/f3de-a8c6-464f-8166-9fd4
https://example2.com/v1/test/14nf-d7jc-54lf-fd90-fds8
https://example3.com/v1/test/bd38-17gd-2h65-0j3b-4jf6
这将取代https://,但是,由于这些值可能会更改,因此不能硬编码每个值的其余部分
要从
/v1/
和https://
之间以及之后剪切任何内容,可能需要进行哪些更改代码更改?这将删除“/v1/”之后的任何内容,并自行删除。这就是你想要的吗
$string = "https://example1.com/v1/test/f3de-a8c6-464f-8166-9fd4"
$string = $string -replace "https://"
$pos = $string.IndexOf("/v1/")
$result = $string.Substring(0, $pos)
$result
Output: example1.com
请注意在结果列表中潜在地汇总您需要的信息。这是我应该做的,但在阅读了@DavidBrabant的答案后,OP肯定应该这样做。
$string = "https://example1.com/v1/test/f3de-a8c6-464f-8166-9fd4"
$string = $string -replace "https://"
$pos = $string.IndexOf("/v1/")
$result = $string.Substring(0, $pos)
$result
Output: example1.com
$list = @(
"https://example1.com/v1/test/f3de-a8c6-464f-8166-9fd4",
"https://example2.com/v1/test/14nf-d7jc-54lf-fd90-fds8",
"https://example3.com/v1/test/bd38-17gd-2h65-0j3b-4jf6"
)
$result = $list | %{
$uri = [System.Uri] $_
$uri.Authority
}
$result