简体   繁体   中英

Parse guids out of a string in powershell

I am new to both powershell and guids. All the examples discussed on the web are for validating a guid. I couldn't find examples of parsing a guid pattern out of string. The regex for a guid is

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

Say I have a string with

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

I want to parse this string to get the first occurrence of the guid, which is {FEB375AB-6EEC-3929-8FAF-188ED81DD8B5}. How can I do this in powershell.

I tried the following. But it is not working:

$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

Wondering if there is something wrong with the expression or the way I am calling it.

I know I'm late to this party, but the System.Guid class provides its own parser. It's pretty easy to use. It also accounts for various accepted guid formats.

$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.

Unfortunately, the references don't work well in powershell, so you'll need to follow that with actually parsing the 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
}

Or you could just use try/catch to see if it parses or not.

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

To demonstrate all the formats it can use, you can do something like this:

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

Many ways to do this. A simple one could be with Select-String with a simplified regex.

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

This would match between curly braces as long as it contains hex characters and hyphens. Not as specific as what you had before but it is easier to understand. Since I don't know what version of PowerShell you are using getting the values out of select-string results is safely done this way.

Originally I was just blinded by the regex length and did not notice what PetSerAl pointed out. You have the start of string and end of string anchors in your regex which would not match your testing string let alone multiple values.

Even removing those you only get the one result from $guid . To get multiple results you need to use a different method.

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

My way:

$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}}'

Then $matches[0] will have the first guid.

Personally I find regular expressions great, however if I find that if I can get the system to do the work then I'll choose that solution instead.

So for GUID's I'd do something along the line of:

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

So I hope that helps some of you guys, Porky

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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