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

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -