如何为每场比赛生成唯一的 Guids
How do I generate Unique Guids for each Match
我正在尝试编写一个脚本,在找到匹配项时生成新的 GUID。我的问题是我一直在为所有匹配项生成相同的 GUID。
如何在不为所有匹配生成相同 GUID 的情况下执行此操作?
$testString = @"
[assembly: Guid Should Replace]
[assembly: Guid Should Replace]
[assembly: Guid Should Replace]
"@
#expected output
#[assembly: "unique guid"]
function ReplaceWithNewGuid {
param($content)
$retval = ($content -ireplace '(?m)(\[assembly: Guid.*$)+', "[assembly: Guid(`"$([guid]::NewGuid())`"]`)")
return $retval
}
ReplaceWithNewGuid($testString)
实际输出示例:
[程序集:Guid("29e784aa-ba4a-4a45-85b8-d6b52916b539"])
[程序集:Guid("29e784aa-ba4a-4a45-85b8-d6b52916b539"])
[程序集:Guid("29e784aa-ba4a-4a45-85b8-d6b52916b539"])
更新
@Mathias R. Jessen 的回答帮助我得到了我需要的东西。我想我可以在不使用 .net 框架库的情况下在 powershell 中执行此操作,但是,这按预期工作。
function ReplaceWithNewGuid {
param($content)
$retval = [regex]::Replace($testString, '(?m)(\[assembly: Guid.*$)+', {param($m) return "[assembly: Guid(`""+ (New-Guid).Guid + "`")]"}, 'IgnoreCase')
return $retval
}
(New-Guid).Guid
Presto,Guid
点播。
并且您的功能已修复:
function ReplaceWithNewGuid
{
param($content)
$retval = ForEach ($Line in ($content -split "`n"))
{
$Line -replace 'guid.*$',"Guid(`"$((New-Guid).Guid)`"]"
}
Return $retval
}
您可以直接使用 Regex.Replace()
,允许您传递一个脚本块来代替 MatchEvaluator 委托参数:
$testString = @"
[assembly: Guid Should Replace]
[assembly: Guid Should Replace]
[assembly: Guid Should Replace]
"@
[regex]::Replace($testString, 'Guid Should Replace', {param($m) return (New-Guid).Guid}, 'IgnoreCase')
您应该看到它 returns 3 个不同的标识符
我正在尝试编写一个脚本,在找到匹配项时生成新的 GUID。我的问题是我一直在为所有匹配项生成相同的 GUID。 如何在不为所有匹配生成相同 GUID 的情况下执行此操作?
$testString = @"
[assembly: Guid Should Replace]
[assembly: Guid Should Replace]
[assembly: Guid Should Replace]
"@
#expected output
#[assembly: "unique guid"]
function ReplaceWithNewGuid {
param($content)
$retval = ($content -ireplace '(?m)(\[assembly: Guid.*$)+', "[assembly: Guid(`"$([guid]::NewGuid())`"]`)")
return $retval
}
ReplaceWithNewGuid($testString)
实际输出示例:
[程序集:Guid("29e784aa-ba4a-4a45-85b8-d6b52916b539"])
[程序集:Guid("29e784aa-ba4a-4a45-85b8-d6b52916b539"])
[程序集:Guid("29e784aa-ba4a-4a45-85b8-d6b52916b539"])
更新
@Mathias R. Jessen 的回答帮助我得到了我需要的东西。我想我可以在不使用 .net 框架库的情况下在 powershell 中执行此操作,但是,这按预期工作。
function ReplaceWithNewGuid {
param($content)
$retval = [regex]::Replace($testString, '(?m)(\[assembly: Guid.*$)+', {param($m) return "[assembly: Guid(`""+ (New-Guid).Guid + "`")]"}, 'IgnoreCase')
return $retval
}
(New-Guid).Guid
Presto,Guid
点播。
并且您的功能已修复:
function ReplaceWithNewGuid
{
param($content)
$retval = ForEach ($Line in ($content -split "`n"))
{
$Line -replace 'guid.*$',"Guid(`"$((New-Guid).Guid)`"]"
}
Return $retval
}
您可以直接使用 Regex.Replace()
,允许您传递一个脚本块来代替 MatchEvaluator 委托参数:
$testString = @"
[assembly: Guid Should Replace]
[assembly: Guid Should Replace]
[assembly: Guid Should Replace]
"@
[regex]::Replace($testString, 'Guid Should Replace', {param($m) return (New-Guid).Guid}, 'IgnoreCase')
您应该看到它 returns 3 个不同的标识符