title: powershell replace data in files sub directory (ok) only if line begins with some specific string (ko)
body: <p>it s ok to replace a string in all files sub directory  : </p>
  
  <p>initial in line : 02#APPLICATION/PDF;name="<strong>P31F9B000A00001.pdf</strong>"*
  replace by  : #
  P31F9B000A00001 can be anything</p>
  
  <p>code : </p>
  
  <pre><code>$array = @("D:\cebi")
  $myRegExp1 = [regex]"#APPLICATION/PDF;name=""(.*).pdf""\*"
  $myRegToReplace1 = "#"
  
  foreach ($element in $array) {
      Get-ChildItem $element  -Recurse -Include "*.DAT" | foreach-object {Copy-Item $_.fullname ($_.fullname+".bak") -recurse } 
      gci $element -r -include "*.DAT"| foreach-object { $a = $_.fullname; ( get-content $a ) |  foreach-object { $_ -replace $myRegExp1,$myRegToReplace1 -replace $myRegExp2,$myRegToReplace2  }  | set-content $a } 
  </code></pre>
  
  <p>}</p>
  
  <p>But now, i would like do replace a string only for the lines  begins by a specific string</p>
  
  <p>i want to replace string "ClasseAutomatique" by "ok_man" only if line begins with "02#APPLICATION/PDF;name="
  intial string : </p>
  
  <pre><code>02#APPLICATION.PDF;name="anything_data.pdf"-anything_data.2016.##_ClasseAutomatique_2016__##anything_data.pdf
  01#APPLICATION.PDF;name="anything_data.pdf"-anything_data.2016.##_ClasseAutomatique_2016__##anything_data.pdf
  </code></pre>
  
  <p>Result would be : </p>
  
  <pre><code>02#APPLICATION.PDF;name="P31F9B000A00001.pdf"-anything_data.2016.##_ok_man_2016__##P31F9B000A00001.pdf
  01#APPLICATION.PDF;name="P31F9B123D02651.pdf"-anything_data.2016.##_ClasseAutomatique_2016__##P31CKB000D00002.pdf
  </code></pre>
  
  <p>i try this  : </p>
  
  <pre><code>$myRegExp3 = [regex]"^(02#APPLICATION/PDF;name=(.*).pdf)-(.*).2016.##_ClasseAutomatique_2016__##(.*)"
  
  $myRegToReplace3 = "$1-$2.2016.##_ok_man_2016__##$3"
  
  $1 would be 02#APPLICATION/PDF;name=(.*).pdf
  $2 would be data in (.*) 
  $3 would be  (.*)
  </code></pre>
  
  <p>but nothing. if someone can have a solution to build the line with replacement</p>