Remove all after

Welcome Forums General PowerShell Q&A Remove all after

Viewing 3 reply threads
  • Author
    • #188479
      Topics: 1
      Replies: 0
      Points: 12
      Rank: Member



      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 Philipp

    • #188515
      Topics: 9
      Replies: 1277
      Points: 1,051
      Helping Hand
      Rank: Community Hero

      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
    • #188521
      Topics: 0
      Replies: 26
      Points: 124
      Helping Hand
      Rank: Participant

      Just try with below regex


      and look for $Matches[1]

    • #188557
      Topics: 5
      Replies: 277
      Points: 170
      Helping Hand
      Rank: Participant
      $string = "REF:,user1,user2,user2,:REF\randomtext"
      $string -match 'REF:,(.*),:REF' | Out-Null ; $Matches[1] -split ','
Viewing 3 reply threads
  • You must be logged in to reply to this topic.