[英]Using PowerShell script to replace build number in DLL version info
I am new to PowerShell and it seems like a reasonable way to replace the build number in my DLLs (by modifying the .rc files) with the revision number from my SVN repository. 我是PowerShell的新手,这似乎是一种合理的方法,用SVN存储库中的修订号替换DLL中的内部版本号(通过修改.rc文件)。
Using PowerShell ISE, I created the following variables: 使用PowerShell ISE,我创建了以下变量:
$1 = "123"
$revision = "230"
Now I am testing my string replacement with the following commands to see how it behaves vs. what I have been reading online. 现在,我正在使用以下命令测试字符串替换,以查看其行为与在线阅读的内容之间的关系。 I am expecting to find something that will give me back a file version of
12,3,0,230
: 我期望找到可以使我返回文件版本
12,3,0,230
:
$pattern = '(^\s*FILEVERSION\s*[0-9]+,[0-9]+,[0-9]+,)[0-9]+$'
' FILEVERSION 12,3,0,0' -replace $pattern, "`$1"
# returns " FILEVERSION 12,3,0,"
' FILEVERSION 12,3,0,0' -replace $pattern, "$1"
# returns "123"
' FILEVERSION 12,3,0,0' -replace $pattern, '$1'
# returns " FILEVERSION 12,3,0,"
' FILEVERSION 12,3,0,0' -replace $pattern, "`$1$revision"
# returns "$1230"
' FILEVERSION 12,3,0,0' -replace $pattern, "`$1`$revision"
# returns " FILEVERSION 12,3,0,$revision"
' FILEVERSION 12,3,0,0' -replace $pattern, "$revision`$1"
# returns "230 FILEVERSION 12,3,0,"
I am surprised by the results from attempts 4 and 6. With attempt 4, why is PowerShell not replacing $revision
with "230"? 我对尝试4和6的结果感到惊讶。为什么使用尝试4,PowerShell不将
$revision
替换$revision
“ 230”? Also with attempt 6, why is it getting replaced properly when it comes before `$1
? 同样在尝试6中,为什么在
`$1
之前将其正确替换? I looked for information about scoping within the replacement string for backticks, and didn't find anything. 我在反引号的替换字符串中查找有关范围界定的信息,但未找到任何内容。
In a replacement operation PowerShell first expands the PowerShell variables before handing the match and replacement strings off to the regular expression engine. 在替换操作中,PowerShell首先扩展PowerShell变量,然后再将匹配和替换字符串交给正则表达式引擎。 Hence the replacement string
"`$1$revision"
is expanded to "$1230"
and passed to the regexp engine. 因此,替换字符串
"`$1$revision"
被扩展为"$1230"
并传递给regexp引擎。 Since there is no 1230th captured group to be expanded the backreference remains as-is. 由于没有第1230个捕获组要扩展,因此后向引用保持原样。
To avoid this issue put the backreference identifier in curly brackets: 为避免此问题,请将后向引用标识符放在大括号中:
'FILEVERSION 12,3,0,0' -replace '...', "`${1}$revision"
That ensures your replacement string consists of the backreference $1
and the string 230
. 这样可以确保您的替换字符串由反向引用
$1
和字符串230
。
Notes about what the other approaches are doing (for the sake of completeness): 关于其他方法在做什么的注释(为了完整性):
' FILEVERSION 12,3,0,0' -replace '...', "`$1"
Returns " FILEVERSION 12,3,0,"
because the entire match is replaced with the backreference to the first capturing group and nothing else. 返回
" FILEVERSION 12,3,0,"
因为整个匹配项都替换为对第一个捕获组的向后引用,而没有其他任何内容。
' FILEVERSION 12,3,0,0' -replace '...', "$1"
Returns "123"
because PowerShell expands the variable $1
before it's passed to the regexp engine. 返回
"123"
因为PowerShell在将变量 $1
传递给正则表达式引擎之前对其进行了扩展。 The latter just sees the string "123"
. 后者仅看到字符串
"123"
。
' FILEVERSION 12,3,0,0' -replace '...', '$1'
Same behavior as with the first approach (the single quotes prevent PowerShell from expanding the variable $1
just like escaping the $
does). 相同的行为与第一种方法(单引号防止PowerShell与扩大变量
$1
就像逃离$
一样)。
' FILEVERSION 12,3,0,0' -replace '...', "`$1`$revision"
Returns " FILEVERSION 12,3,0,$revision"
because both $
are escaped, so PowerShell doesn't expand $revision
, and for the regexp engine $revision
is just an ordinary string. 返回
" FILEVERSION 12,3,0,$revision"
因为两个$
均被转义,因此PowerShell不会扩展$revision
,对于正则表达式引擎$revision
只是一个普通字符串。
' FILEVERSION 12,3,0,0' -replace '...', "$revision`$1"
Returns "230 FILEVERSION 12,3,0,"
because PowerShell expands $revision
, but this time the value stands before the backreference, so the regexp engine can expand $1
to the first captured group. 返回
"230 FILEVERSION 12,3,0,"
因为PowerShell扩展了$revision
,但是这次值位于反向引用之前,因此regexp引擎可以将$1
扩展到第一个捕获的组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.