Remove all after



I´ve got a problem with a powershell script which i user for a nagios check.

the script gets an argument from nagios and need to format it correctly.

I get “REF:,user1,user2,user2,:REF\randomtext” from the nagios.


Now i need to ignore everything bevore and after the :REF / REF: (also the REF), i need only the users.

I´ve tried it with regex (?<=REF:)(.*)(?=:REF) but i cant use the output correctly.


maybe someone got an idea how i could fix this?


kind regards


What exactly isn’t working? If the leading and ending common is in every output, you could probably update the regex, but it’s simple to parse null items in the array.

$text = "REF:,user1,user2,user2,:REF\randomtext" 

$text -match '(?<=REF:)(.*)(?=:REF)' #Perform Match

$Matches[0] #,user1,user2,user2,

$users = $Matches[0] -split ',' | Where{$_} #Convert to array


Just try with below regex


and look for $Matches[1]

$string = "REF:,user1,user2,user2,:REF\randomtext"

$string -match 'REF:,(.*),:REF' | Out-Null ; $Matches[1] -split ','