Reputation: 37
Powershell 7.0 Script: I want to reverse a regex that I used in 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.
I want that the string after the command is A00B00
and not random random
.
I tried diffrent regex but nothing worked for me
Upvotes: 0
Views: 1123
Reputation: 5242
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:
'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'
Upvotes: 3