title: Powershell Regex: Reading a multi-line string between two points
body: <p>I have a common enough problem with a powershell regex to read multi-line records. I've read the threads asking similar questions but can't quite get the solutions to work in my case. </p>
  
  <p>My file consists of multi-line records of variable length. The records I am interested in start with a 01 or a 02 followed by a V or a M. The record ends whenever another record begins or when a batch record starting with '50' is found. The first three characters of each line identifies the record.</p>
  
  <p>ie 
  01V  (Start of record - content follows here)
  01
  
  <p>I'm trying to read the individual records with a regex by identifying the start and the end. </p>
  
  <p>What I have at the moment is based off this answer:
  <a href="https://stackoverflow.com/questions/25639252/match-everything-between-two-words-in-powershell">Match everything between two words in Powershell</a></p>
  
  <pre><code>#Read the file as a single string
  $FilePath = "blaablaablaa"
  $TestFile = get-content $FilePath | Out-String 
  
  #( ?= Assert that this matches before the current position
  # 0(1|2)(V|M) if the record is 01V or 01M or 02V or 02M 
  # ) End assertion 
  # .+? Match any number of characters, few as possible
  # (?= Until a record starting with 70 is found  
  # ) End of look ahead
  $regex = [regex] '(?is)(?&lt;=0(1|2)(V|M)).+?(?=70)'
  echo $TestFile |  select-string -Pattern $regex 
  </code></pre>
  
  <p>The above will work with single lines strings if I remove the pipe to out-sting with with the out-string pipe it returns the entire file. I'm guessing I'm not handling the /n characters correctly. </p>
  
  <p>Any advice? The input file looks roughly like this:</p>
  
  <blockquote>
    <p>00               date<br>
    01Mxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br>
    01
    01
    01
    01=0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br>
    01=5xxxxxxxxxxxxxxxxxxxxxxxxxxx<br>
    01Mxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br>
    01
    01
    01
    01=0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br>
    01=9xxxxxxxxxxxxxxxxxxxxxxxxxxx<br>
    50 xxxxxxxxxxxxx             xxxxxxxxxxxxxxxxx<br>
    01Vxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br>
    01$1      xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br>
    01$A         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br>
    01$B                              0xxxxxxxxxxxxxxxxxxxx<br>
    01$0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br>
    01$5xxxxxxxxxxxxxxxxxxxxxxxxxxx<br>
    50 xxxxxxxxxxxx             BatchTotal<br>
    90 xxxxxxxxxxxx             FILETotal             </p>
  </blockquote>
  
  <p>The required output is splitting out the file into individual records which are delimited by a '50' or a '90' or the start of another record. This for example is the final record :-</p>
  
  <blockquote>
    <p>01Vxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br>
    01$1      xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br>
    01$A         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br>
    01$B                              0xxxxxxxxxxxxxxxxxxxx<br>
    01$0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br>
    01$5xxxxxxxxxxxxxxxxxxxxxxxxxxx   </p>
  </blockquote>