title: Parse EML text With Regular Expression
body: <p>Could you help me please parse EML text with regular expression. </p>
  
  <p>I want to get separately:</p>
  
  <p>1). text between  Content-Transfer-Encoding: base64 and --=_alternative, if there is above line Content-Type: text/html </p>
  
  <p>2). text between  Content-Transfer-Encoding: base64 and --=_related, if there is two lines above line Content-Type: image/jpeg</p>
  
  <p>Take a look, please, on peace of code in powershell:</p>
  
  <pre><code>$text = @"
  --=_alternative XXXXXXXXXXXXXX_=
  Content-Type: text/html; charset="KOI8-R"
  Content-Transfer-Encoding: base64
  
  111111111111111111111111111111111111111111111111111111
  
  --=_alternative XXXXXXXXXXXXXX_=
  Content-Type: text/html; charset="KOI8-R"
  Content-Transfer-Encoding: base64
  
  222222222222222222222222222222222222222222222222222222
  --=_alternative XXXXXXXXXXXXXX_=--
  --=_related XXXXXXXXXXXXXX_=--_=
  Content-Type: image/jpeg
  Content-ID: &lt;_2_XXXXXXXXXXXXXX&gt;
  Content-Transfer-Encoding: base64
  
  333333333333333333333333333333333333333333333333333333
  --=_related XXXXXXXXXXXXXX_=
  Content-Type: image/jpeg
  Content-ID: &lt;_2_XXXXXXXXXXXXXX&gt;
  Content-Transfer-Encoding: base64
  444444444444444444444444444444444444444444444444444444
  
  --=_related XXXXXXXXXXXXXX_=
  Content-Type: image/jpeg
  Content-ID: &lt;_2_XXXXXXXXXXXXXX&gt;
  Content-Transfer-Encoding: base64
  
  555555555555555555555555555555555555555555555555555555
  --=_related XXXXXXXXXXXXXX_=--
  "@
  
  $regex1 = "(?ms).+?Content-Transfer-Encoding: base64(.+?)--=_alternative"
  $text1 = ([regex]::Matches($text,$regex1) | foreach {$_.groups[1].value})
  Write-Host "text1 : " -fore red
  Write-Host  $text1
  
  #I want to get as output elements (of array, maybe, or one after another)
  #1). text between  Content-Transfer-Encoding: base64 and --=_alternative, if there is above line Content-Type: text/html
  #this
  #1111111111111111111111111111111111111111111111111111111
  #then this
  #2222222222222222222222222222222222222222222222222222222
  
  $regex2 = "(?ms).+?Content-Transfer-Encoding: base64(.+?)--=_related"
  $text2 = ([regex]::Matches($text,$regex2) | foreach {$_.groups[1].value})
  #I want to get as output elements (of array, maybe, or one after another)
  #2). text between  Content-Transfer-Encoding: base64 and --=_related, if there is two lines above line Content-Type: image/jpeg
  #this
  #3333333333333333333333333333333333333333333333333333333
  #then this
  #4444444444444444444444444444444444444444444444444444444
  #then this
  #5555555555555555555555555555555555555555555555555555555
  Write-Host "text2 : " -fore red
  Write-Host  $text2
  </code></pre>
  
  <p>Thanks for your help. Have a nice day.</p>
  
  <p>P.S. Based on code of Jessie Westlake, here is a little edited version of RegEx, that worked for me:</p>
  
  <pre><code>$files = Get-ChildItem -Path "\\&lt;SERVER_NAME&gt;\mailroot\Drop"
  Foreach ($file in $files){
      $text = Get-Content $file.FullName
  
      $RegexText = '(?:Content-Type: text/html.+?Content-Transfer-Encoding: base64(.+?)(?:--=_))'
      $RegexImage = '(?:Content-Type: image/jpeg.+?Content-Transfer-Encoding: base64(.+?)(?:--=_))'
  
      $TextMatches = [Regex]::Matches($text, $RegexText, [System.Text.RegularExpressions.RegexOptions]::Singleline)
      $ImageMatches = [Regex]::Matches($text, $RegexImage, [System.Text.RegularExpressions.RegexOptions]::Singleline)
  
      If ($TextMatches[0].Success)
      {
          Write-Host "Found $($TextMatches.Count) Text Matches:"
          Write-Output $TextMatches.ForEach({$_.Groups[1].Value})
      }
      If ($ImageMatches[0].Success)
      {
          Write-Host "Found $($ImageMatches.Count) Image Matches:"
          Write-Output $ImageMatches.ForEach({$_.Groups[1].Value})
      }
  }
  </code></pre>