[英]PowerShell Regex with “.” PowerShell
i want to replace a part of some Strings in a loop with PowerShell 我想用PowerShell在循环中替换部分字符串的一部分
example string 示例字符串
testvm029.vmxxx
I want to replace everything at .vmxxx. 我想替换.vmxxx中的所有内容。 Every string has another length but all ends with
.vm...
So the result should be: testvm029
每个字符串都有另一个长度,但是所有结尾都是
.vm...
因此结果应该是: testvm029
I tried the following Script: 我尝试了以下脚本:
Foreach($String in $Strings) {
$StringTest = $String -replace "(.vm)(.+)","$null"
}
Of course this kills my String on the first vm
and not at .vm...
result: test
当然,这杀死了我的第一个String
vm
,而不是在.vm...
结果: test
how can i achieve my goal? 我如何实现我的目标?
EDIT: .vm is not the last '.' 编辑:.vm不是最后一个'。 of my String so something like that:
test123.vmabc.string
我的字符串,像这样:
test123.vmabc.string
I want to cut it after .vm
我想在
.vm
之后将其.vm
You need to use 您需要使用
$String -replace '\.vm.*'
See the regex demo 见正则表达式演示
The pattern will find the first .vm
substring (with \\.vm
) and then match the rest of the string (with .*
). 该模式将找到第一个
.vm
子字符串(带有\\.vm
),然后匹配字符串的其余部分(带有.*
)。 The match will be replaced with an empty string (it is used by default, but you may write it explicitly, $String -replace '\\.vm.*', ''
). 匹配项将替换为空字符串(默认情况下使用该字符串,但您可以显式编写它,
$String -replace '\\.vm.*', ''
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.