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


    • #188515
      Topics: 12
      Replies: 1618
      Points: 2,543
      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: 28
      Points: 144
      Helping Hand
      Rank: Participant

      Just try with below regex


      and look for $Matches[1]

    • #188557
      Topics: 5
      Replies: 322
      Points: 444
      Helping Hand
      Rank: Contributor
      $string = "REF:,user1,user2,user2,:REF\randomtext"
      $string -match 'REF:,(.*),:REF' | Out-Null ; $Matches[1] -split ','
Viewing 3 reply threads
  • The topic ‘Remove all after’ is closed to new replies.