Answer the question
In order to leave comments, you need to log in
Extra spaces when regex replaced in PowerShell?
There is a file in which values are written through | . Looks like this
PARAM_ONE|1|N
PORT|1|502
PARAM_TWO|1|0
Answer the question
In order to leave comments, you need to log in
"PORT|1|502" -creplace "(PORT\|\d+\|)(\d+)", '${1}500'
"PORT|1|502" -creplace "(?<=PORT\|\d+\|)\d+", '500'
a crutch of course, but it works. just need some unique delimiter and another replace run:
"PORT|1|502" -replace "(PORT\|\d+\|)(\d+)","`$1 500" -replace " "
or
"PORT |1|502" -replace "(PORT\|\d+\|)(\d+)","`$1zhopa500" -replace "zhopa"
UPD: replaced single quotes with double quotes and added back quote to $1
UPD: like this works:
$port="500"
"PORT|1|502" -replace "(PORT\|\d+)\|(\d+)","`$1|$port"
Получилось ещё вот так:
"PORT|1|502" -creplace "(PORT\|\d+)\|(\d+)",'$1|500'
Но это какой то костыль все равно.
Проблема в том что в строке замены нельзя написать $1500 т.к. аргументы нельзя разделить (цифры объединяются) Может можно как то переменную указать вместо второго аргумента $1$port но так переменную $port не видно.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question