having difficulty with regex replacing multiple lines in a variable

Welcome Forums General PowerShell Q&A having difficulty with regex replacing multiple lines in a variable

Viewing 2 reply threads
  • Author
    Posts
    • #188668
      Participant
      Topics: 2
      Replies: 1
      Points: 29
      Rank: Member

      why does this script remove multiple lines

      str =
      @"
      123
      abc
      456
      xyz
      789
      "@
      $str -replace "(?ms).*45",""
      

      and output this as expected

      6
      xyz
      789

      but this script removes only the beginning of many lines

      `$str = schtasks /query /fo csv`
      `$str -replace "(?ms).*\\.*\\",""`

    • #188704
      Participant
      Topics: 1
      Replies: 1632
      Points: 3,074
      Helping Hand
      Rank: Community Hero

      Could you please use the code tag button (pre) for your code next time? Thanks.

      In your first case there's one single string with line breaks (a so called "here string") and the second case is about multiple strings (an array of strings) ... like this ...

      $str = @(
      '123'
      'abc'
      '456'
      'xyz'
      '789'
      )
      $str -replace "(?ms).*45", ""

      ... a kind of

    • #188716
      Participant
      Topics: 2
      Replies: 1
      Points: 29
      Rank: Member

      sorry for the faux pas. I mistakingly thought format->code was the right way to format code

Viewing 2 reply threads
  • You must be logged in to reply to this topic.