gpt4 book ai didi

string - Powershell字符串操作和替换

转载 作者:行者123 更新时间:2023-12-03 00:45:32 25 4
gpt4 key购买 nike

我有一个可以包含多个电子邮件地址的powershell字符串,例如,下面是一个包含两个电子邮件ID的示例。在这种情况下,我有两种情况。

1) Scenario One where the @gmail.com is consistent
strEmail=john.roger@gmail.com,smith.david@gmail.com

2) Secnario second: where the @mail.com could be different
strEmail2 = john.roger@gmail.com,smith.david@outlook.com

我需要清除@之后的所有内容。
So for result for 
scenario (1) will be: john.roger,smith.david
Scenario (2) will be: john.roger,smith.david

因此,对于Scenarion(1),我可以使用替换为“@ gmail.com”的“硬编码”值,第二个secnario怎么样。

我正在寻找一些适用于这两种情况的解决方案...就像正则表达式中的某些内容或我不知道的其他任何方式。

最佳答案

拆分和合并将在一行上返回名称

以下

$strEmail = "john.roger@gmail.com,smith.david@outlook.com"
($strEmail -split "," | % {($_ -split "@")[0]}) -join ","

退货
john.roger,smith.david

分解
$strEmail -split ","      returns an array of two elements
[0] john.roger@gmail.com
[1] smith.david@outlook.com

% {($_ -split "@")[0]} loops over the array
and splits each item into an array of two elements
[0] john.roger
[1] gmail.com

[0] smith.david
[1] outlook.com
and returns the first element [0] from each array

- join "," joins each returned item into a new string

关于string - Powershell字符串操作和替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48293494/

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