regex - 在 Powershell 中插入数字替换

标签 regex powershell

我想使用 powershell 将字符串的特定数字部分替换为另一个数字。

最好用一个例子来描述这个问题。

$s = '   First part: 65 Second Part'

###################################################################
# this generates "       First part: sixty-six Second Part"       #
###################################################################
$s -replace '^(\s*First part:\s+)[0-9]+(.*)$', '$1sixty-six$2'

###################################################################
# But I want to generate"    First part: 66 Second Part"          #
###################################################################
$s -replace '^(\s*First part:\s+)[0-9]+(.*)$', '$166$2'

它不起作用。

如何指定“$1 后跟数字 66”而不是“$166”?

最佳答案

使用此语法:${1}66$2

输出:

'   First part: 66 Second Part'

关于regex - 在 Powershell 中插入数字替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9306441/

相关文章:

powershell - PSCustomObject 到哈希表

powershell - start-job 并行运行脚本

javascript string.replace 不工作

windows - IIS 使用 powershell 禁用 ocsp 装订

java - 正则表达式 - 我可以缩短这个表达式吗?

Java 正则表达式字符串解析,试图找出一种模式

python - 从 Python 中的 Flask 路由调用 Linux Powershell 中的 .ps1 脚本

powershell - 如何在Powershell block /闭包参数中使用变量

java - 如何用末尾的空格替换字符串中的最后一个字符

javascript - 如何根据它是 Node.JS 中的 $regex 还是 $text 对 Mongoose 结果进行排序?