繁体   English   中英

从powershell中的字符串中解析guids

[英]Parse guids out of a string in powershell

我是 powershell 和 guids 的新手。 网络上讨论的所有示例都用于验证 guid。 我找不到从字符串中解析 guid 模式的示例。 guid 的正则表达式是

^{[A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}}$

说我有一个字符串

"This is a sample string with two guids. Guid1 is {FEB375AB-6EEC-3929-8FAF-188ED81DD8B5}. Guid2 is {B24E0C46-B627-4781-975E-620ED53CD981}"

我想解析此字符串以获取第一次出现的 guid,即 {FEB375AB-6EEC-3929-8FAF-188ED81DD8B5}。 我怎样才能在powershell中做到这一点。

我尝试了以下方法。 但它不起作用:

$fullString = "This is a sample string with two guids. Guid1 is {FEB375AB-6EEC-3929-8FAF-188ED81DD8B5}. Guid2 is {B24E0C46-B627-4781-975E-620ED53CD981}"

$guid = [regex]::match($fullString, '^{[A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}}$')
Write-Host $guid.Groups[1].Value

想知道表达方式或我调用它的方式是否有问题。

我知道我迟到了,但是 System.Guid 类提供了它自己的解析器。 它很容易使用。 它还解释了各种公认的 guid 格式。

$Result = [System.Guid]::empty #Reference for the output, required by the method but not useful in powershell
[System.Guid]::TryParse("foo",[System.Management.Automation.PSReference]$Result) # Returns true if successfully parsed and assigns the parsed guid to $Result, otherwise false.

$Result = [System.Guid]::empty #Reference for the output, required by the method but not useful in powershell
[System.Guid]::TryParse("12345678-1234-1234-1234-987654321abc",[System.Management.Automation.PSReference]$Result) # Returns true if successfully parsed, otherwise false.

不幸的是,这些引用在 powershell 中不能很好地工作,因此您需要在实际解析 guid 时遵循这一点。

$string = "12345678-1234-1234-1234-987654321abc"
$Result = [System.Guid]::empty
If ([System.Guid]::TryParse($string,[System.Management.Automation.PSReference]$Result)) {
$Result = [System.Guid]::Parse($string)
} Else {
$Result = $null
}

或者您可以使用 try/catch 来查看它是否解析。

$string = "12345678-1234-1234-1234-987654321abc"
Try { $Result = [System.Guid]::Parse($string) } Catch { $Result =  $Null } Finally { $Result }

要演示它可以使用的所有格式,您可以执行以下操作:

$guid = [guid]"12345678-1234-1234-1234-987654321abc"
"d","n","p","b","x" | ForEach-Object { $guid.tostring($_) }

有很多方法可以做到这一点。 一个简单的方法是使用带有简化正则表达式的Select-String

$fullString | Select-String -Pattern '{[-0-9A-F]+?}' -AllMatches | Select-Object -ExpandProperty Matches | Select-Object -ExpandProperty Value

只要它包含十六进制字符和连字符,它就会在大括号之间匹配。 不像以前那样具体,但更容易理解。 由于我不知道您使用的是哪个版本的 PowerShell,因此可以通过这种方式安全地从select-string结果中获取值。

最初我只是被正则表达式的长度蒙蔽了双眼,并没有注意到 PetSerAl 指出了什么。 您的正则表达式中有字符串的开头和字符串的结尾锚点,它们与您的测试字符串不匹配,更不用说多个值了。

即使删除那些你也只能从$guid得到一个结果。 要获得多个结果,您需要使用不同的方法。

[regex]::Matches($fullString,'{([-0-9A-F]+?)}')

我的方式:

$string = "This is a sample string with two guids. Guid1 is {FEB375AB-6EEC-3929-8FAF-188ED81DD8B5}. Guid2 is {B24E0C46-B627-4781-975E-620ED53CD981}" 
$string -match '{\w{8}-\w{4}-\w{4}-\w{4}-\w{12}}'

然后 $matches[0] 将拥有第一个 guid。

我个人认为正则表达式很棒,但是如果我发现如果我可以让系统完成这项工作,那么我会选择该解决方案。

所以对于 GUID,我会做一些类似的事情:

If([System.Guid]::Parse($Majic_GUID_String_To_Test) -is [Guid] ) { Then continue programming ... }

所以我希望对你们中的一些人有所帮助,Porky

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM