title: How to create a powershell script that uses regex to replace a string in text with a tab
body: <p>I have a string like this:</p>
  
  <p><code>"Item 1","Item 2"</code></p>
  
  <p>I would like to replace it so that it looks like this, using a powershell script:</p>
  
  <p><code>Item 1{tab character}Item 2</code></p>
  
  <p>I have this:</p>
  
  <pre><code>$text = '"Item 1","Item 2"'
  $expr1 = '"([^"]+?)","([^"]+?)"'
  $expr2 = "$1\t$2"
  $line = [System.Text.RegularExpressions.Regex]::Replace($text, $expr1, $expr2);
  </code></pre>
  
  <p>but it doesn't work.</p>
  
  <p>As an aside, is there a definitive reference for how to deal with escaping quotes and special characters in Powershell? I find it very confusing indeed.</p>
  
  <p>.</p>
  
  <p>.  </p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>The reason I want to do this is so that I can wrap this up in a parameterised script and call it using parameters. The script (regex-rs.ps1) is like this:</p>
  
  <pre><code>param
  (
      [string] $text,
      [string] $inputPattern,
      [string] $replacePattern
  )
  
  function Main()
  {
      $text2 = [System.Text.RegularExpressions.Regex]::Replace($text, $inputPattern, $replacePattern);
      [System.Console]::WriteLine($text2);
  }
  
  Main;
  </code></pre>
  
  <p>Unfortunately, when I call the script like this:</p>
  
  <pre><code>powershell .\regex-rs.ps1 '"Text1","Text2"' '`"([^`"]+?)`",`"([^`"]+?)`"' '`$1`t`$2'
  </code></pre>
  
  <p>It outputs:</p>
  
  <pre><code>Text1,Text2
  </code></pre>
  
  <p>In other words, no tab. What am I doing wrong?</p>
  
  <p>.</p>
  
  <p>.</p>
  
  <p><strong>FURTHER EDIT IN RESPONSE TO NICK'S ANSWER BELOW:</strong> (I have to put this here, because the comment formatting in StackOverflow messes around with backticks)</p>
  
  <p>I replaced the single quotes with double quotes in my powershell call, like so:</p>
  
  <pre><code>powershell .\regex-rs.ps1 ""Text1","Text2"" "`"([^`"]+?)`",`"([^`"]+?)`"" "`$1`t`$2"
  </code></pre>
  
  <p>But I got this error:</p>
  
  <pre><code>Missing ] at end of type token.
  </code></pre>
  
  <p>Any further ideas?</p>
  
  <p>.</p>
  
  <p><strong>FINAL EDIT:</strong> This is the call to the script that fixed the issue (have to post as an image, because it's so powerful that it's defied StackOverflow's formatting, even here):
  .</p>
  
  <p><img src="https://i.stack.imgur.com/yUn8m.jpg" alt="final script call"></p>