The potential duplicate indeed shows the technique needed to implement your intent:
You want to perform a dynamic operation on a part of the matched string, based on its specific value, so as to be able to use a (conditionally) transformed version of it in the output string produced by a -replace-based string-substitution operation.
This can not be done with a string as the substitution operand,[1] and instead requires a callback mechanism; in the context of PowerShell, it requires a script block ({ .. }), whose use with -replace is only supported in PowerShell (Core) 7+, however.
In Windows PowerShell, you must call the underlying .NET API, [regex]::Replace(), directly, as shown in the linked post.
The only substantial difference is in how the script block receives the match-information object (of type [Match]) that represents the matched string:
In PS 7+, with -replace, it is reflected in the automatic $_ variable (aka $PSItem).
With a direct [regex]::Replace() call, it is in the first positional argument, $args[0], via the automatic $args variable - though you're free to declare a parameter instead, as shown in the linked post (param($match)).
However, there's a twist to your requirements that to me warrants answering this question separately:
- The need to not just perform a dynamic transformation, but to do so in the context of matching the input string as a whole and rearranging its - selectively transformed - substrings.
The immediate answer to your question - which is focused on an additional -replace operation, after having rearranged substrings - is the following:
# PS 7+ only
# Make sure that a single digit before '.house' is 0-padded to 2 digits.
# * $_ is a [System.Text.RegularExpressions.Match] instance with information
# about the matched string.
# * $_.Value is the matched string itself, on which .PadLeft() can be called.
# -> 'Prop2119house01.house'
'Prop2119house1.house' -replace '(?<!\d)\d\b', { $_.Value.PadLeft(2, '0') }
Note:
- This is essentially the same technique as in the linked post, but with the simplification of using a negative lookbehind assertion (
(?<!...)) and a word-boundary assertion to (\b) so as to avoid having to use capture groups ((...)) - see this regex101.com page for an explanation of the regex and the option to experiment with it.
However, you can use this technique to make do with a single -replace operation, which combines rearranging substrings with selective dynamic transformations, based on matching and replacing the whole input string, with the substring of interest captured via capture groups ((...)):
# PS 7+ only
# Sample input lines.
$lines = 'Prop.houses-2119-1', 'Prop.houses-2119-03'
# * $_.Groups[1].Value is the text captured by the 1st capture group, and so on.
# -> 'Prop2119house01.house', 'Prop2119house03.house'
$lines -replace 'Prop\.houses-(\d+)-(\d+)', {
'Prop{0}house{1}.house' -f $_.Groups[1].Value, $_.Groups[2].Value.PadLeft(2, '0')
}
Note the use of -f, the format operator, to assemble the final string; in the template string, {0} is a placeholder for the first RHS operand, and so on.
[1] For a detailed explanation, see the bottom section of this answer