title: Translating C# regex into Powershell
body: <p>I have this regex that works fine on C#:</p>
  
  <pre><code>\s*use\s+(.*?)([\r]{0,1}\n)
  </code></pre>
  
  <p>I have tried using it (or similar versions of it) on Powershell but it doesn't work.
  Can someone help me ?</p>
  
  <p>Some failed attempts:</p>
  
  <pre><code>$query = [regex]::Replace($query, "\s*use\s+(.*?)([`r]{0,1}`n)", ("USE [" + $database + "]"), [System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::Multiline)
  
  $query = [regex]::Replace($query, "\s*use\s+([^`r]{1,}?)([`r])", ("USE [" + $database + "]$2"), [System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::Multiline)
  
  $query = $query -ireplace "\s*use\s+(.+)([\r]{0,1})", ("USE [" + $database + "]"
  </code></pre>
  
  <p>Some questions:</p>
  
  <ol>
  <li><p>Do I need to use <code>`r`n</code> to get C#'s <code>\r\n</code> ??? (I am not yet familiar with PowerShell...)</p></li>
  <li><p>Since my 1st attempt seems to be using a .NET function, shouldn't this function work as it works on .NET? (because I think I see some differences but I can't pinpoint the cause - is it because of the escape characters?)</p></li>
  <li><p>How is my first regex (presented above) translated into Powershell (either with [regex]::Replace() or with -ireplace)?</p></li>
  </ol>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Data to test against (I use it to change the default DB in SQL scripts):</p>
  
  <pre><code>            CREATE LOGIN [test] WITH PASSWORD=N'j-9sfjhpsojhp', 
              DEFAULT_DATABASE=[test], DEFAULT_LANGUAGE=[us_english], 
              CHECK_EXPIRATION=OFF, CHECK_POLICY=ON
              GO
  
              USE test
              GO
  
              /****** Object:  User [test]    Script Date: 10/07/2011 16:39:48 ******/
              GO
  
              CREATE USER [test] FOR LOGIN [test] WITH DEFAULT_SCHEMA=[dbo]
              GO
  </code></pre>
  
  <p>Using a C# tool I get the <code>USE</code> line correctly (and I can replace it using sth like this : <code>$2USE NewDb$2</code>).</p>
  
  <p>When using this (that uses the same regex) in Powershell, it doesn't work (I still get the <code>USE test</code> printed):</p>
  
  <pre><code>$database = "NewDb"
  $query = Get-Content '&lt;path to sql file&gt;'
  $query = [regex]::Replace($query, '\s*use\s+(.*?)([`r]{0,1}`n)', ("USE [" + $database + "]"), [System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::Multiline)
  $query
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>A working solution:</p>
  
  <pre><code>$database = "NewDb"
  $query = (Get-Content '&lt;file path&gt;') -join "`r`n"
  $query = [regex]::Replace($query, '^\s*use\s+(.*?)$', ("`nUSE [" + $database + "]`n"), [System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::Multiline)
  $query
  </code></pre>
  
  <p>However, newlines still don't work as in the .NET version.</p>