RegEx Pain

Hi,

Im trying to get the hash after the

Commit:

using RegEx and Select-String, but i can’t get it to work - i’m hoping one of you guys can see where im going wrong?

$RegEx = '/Commit:?.*/gm'
Select-String -InputObject $StringOutput -Pattern $RegEx
The value of $StingOutput is:
Comments :
           +  : merge from develop
           
Version  : 
           Commit: 2dfg444dfxbbghd6764ghb15ed8afd9f171
           Build Version: 3.0.981

Thanks

Tommy

Multiline strings are always a bit tricky, especially because in PS they can come in two flavours – a single string, or many strings, one per line.

I wouldn’t use Select-String here, though, in general. It’s good for finding the line your match exists on, but actually retrieving the match is a little trickier in some cases. I like to work with the built in -match operator.

# If the string is one big block, split on newline
$Lines = $StringOutput -split '\r\n?'
$Lines | ForEach-Object {
    if ($_ -match 'Commit: (.+)') {
        $Matches[1]
    }
}

I think, splitting and foreach would not be required here.

$StringOutput -Match 'Commit: (.+)'
$Matches[1]

Thanks for the replies.
I ended up using Kvprasoon solution. I knew there would be a easier way. :slight_smile:

Cheers

Tommy

@TommyQuality : Power of community