PowerShell 替换字符串中的数字 [英] PowerShell Replace number in string

查看:39
本文介绍了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

这篇关于PowerShell 替换字符串中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆