regex - replacing with a numbered value -
this question has answer here:
suppose have string
$t = "some {great} vacation {we} had {year}"
and want replace placeholders numbered value:
"some {0} vacation {1} had {2}"
i'd have expected single replacement it:
$i = 0; $t -replace "{.*?}", "{$([string] ++$i)}"
but is:
"some {1} vacation {1} had {1}"
as if string evaluated once. in perl regex there's way specify function replacement value... how done in psh?
try this:
$i = 0 ; [regex]::matches($t,"{.*?}") | %{ ++$i ; $t = $t.replace($_.value,"{$i}") } ; $t
i think problem regex engine never gets new value of $i each match finds, every time increments it, $i
0
.
Comments
Post a Comment