PowerShell 替换字符串中的数字
问题描述:
我不是这样的 regex
专家,坦率地说,我尽量避免它.
I'm not such a regex
expert and frankly I'm trying to avoid it whenever I can.
我想创建一个新的 $String
,其中字符串中的数字用 +1
更新.该数字可以是一位或两位数字,并且始终位于 2 个括号之间.
I would like to create a new $String
where the number within the string is updated with +1
. This number can be one or two digits and will always be between 2 brackets.
来自:
$String = "\\Server\c$\share_1\share2_\Taget2[1] - 2014-07-29.log"
致:
$String = "\\Server\c$\share_1\share2_\Taget2[2] - 2014-07-29.log"
感谢您的帮助.
答
如果你想避免正则表达式:
If you want to avoid the regex:
$String = "\\Server\c$\share_1\share2_\Taget2[1] - 2014-07-29.log"
$parts = $string.Split('[]')
$Newstring = '{0}[{1}]{2}' -f $parts[0],(1 + $parts[1]),$parts[2]
$Newstring
\\Server\c$\share_1\share2_\Taget2[2] - 2014-07-29.log