Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 386
Romonov Avatar asked Mar 15 '23 15:03

Romonov


2 Answers

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($_) }
like image 182
Jordan Mills Avatar answered Mar 25 '23 06:03

Jordan Mills


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]+?)}')
like image 27
Matt Avatar answered Mar 25 '23 07:03

Matt