[英]Powershell Inverse replace Regex
Powershell 7.0 Script: I want to reverse a regex that I used in replace
. Powershell 7.0 脚本:我想反转我在
replace
中使用的正则表达式。
I have something like this:我有这样的事情:
$a = $_.BaseName -replace 'REGEX', ''
The names here are in this pattern (random are random characters and numbers with different length):这里的名字是这种模式(随机是随机字符和不同长度的数字):
random A00B00 random
where the numbers are all diffrent.数字都是不同的。 I can use
A[0-9][0-9]B[0-9]
to match what I want, but then, I replace only what I want.我可以使用
A[0-9][0-9]B[0-9]
来匹配我想要的,但是,我只替换我想要的。
I want that the string after the command is A00B00
and not random random
.我希望命令后的字符串是
A00B00
而不是random random
。
I tried diffrent regex but nothing worked for me我尝试了不同的正则表达式,但对我没有任何帮助
You have a few options to achive what you're looking for.你有几个选择来实现你正在寻找的东西。
One way to go would be to use regex look around and replace what you not want: go 的一种方法是使用正则表达式环顾四周并替换您不想要的内容:
'random A00B00 random' -replace '(?<=^).*(?=A\d{2}B\d{2})' -replace '(?<=A\d{2}B\d{2}).+'
Or you use a back reference and replace your string with a part of the original string, matched in a regex capture group:或者您使用反向引用并将您的字符串替换为原始字符串的一部分,在正则表达式捕获组中匹配:
'random A00B00 random' -replace '.+(A\d{2}B\d{2}).+','$1'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.