【发布时间】:2022-01-03 09:26:29
【问题描述】:
我想编写一个拆分字符串的正则表达式,例如只选择几个元素。例如:
M:\Shares\Profiles\Server\Profiles\abcd.contoso.V2.01
我的目标是:
abcd.V2.01,这样就去掉了'contoso'这个域名
但是,在找到匹配项后,我无法排除部分字符串。我试过了
$original = 'M:\Shares\Profiles\Server\Profiles\abcd.contoso.V2.01'
$modified = $original -replace '.*\\([^\\.]+.contoso.V2)[^\\]*$', '$1'
返回
$modified 为'abcd.contoso.V2'
【问题讨论】:
-
如果你要替换的东西是一个常数,那么这个 >>>
'M:\Shares\Profiles\Server\Profiles\abcd.contoso.V2.01'.Split('\')[-1] -replace '\.contoso'
标签: regex powershell