regex - Parse guids out of a string in powershell -


i new both powershell , guids. examples discussed on web validating guid. couldn't find examples of parsing guid pattern out of string. regex guid

^{[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}}$ 

say have string

"this sample string 2 guids. guid1 {feb375ab-6eec-3929-8faf-188ed81dd8b5}. guid2 {b24e0c46-b627-4781-975e-620ed53cd981}" 

i want parse string first occurrence of guid, {feb375ab-6eec-3929-8faf-188ed81dd8b5}. how can in powershell.

i tried following. not working:

$fullstring = "this sample string 2 guids. guid1 {feb375ab-6eec-3929-8faf-188ed81dd8b5}. guid2 {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 wrong expression or way calling it.

many ways this. simple 1 select-string simplified regex.

$fullstring | select-string -pattern '{[-0-9a-f]+?}' -allmatches | select-object -expandproperty matches | select-object -expandproperty value 

this match between curly braces long contains hex characters , hyphens. not specific had before easier understand. since don't know version of powershell using getting values out of select-string results safely done way.

originally blinded regex length , did not notice petseral pointed out. have start of string , end of string anchors in regex not match testing string let alone multiple values.

even removing 1 result $guid. multiple results need use different method.

[regex]::matches($fullstring,'{([-0-9a-f]+?)}') 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -