Powershell regex group replacing Powershell regex group replacing powershell powershell

Powershell regex group replacing


Not sure that provided regex for tables names is correct, but anyway you could replace with captures using variables $1, $2 and so on, and following syntax: 'Doe, John' -ireplace '(\w+), (\w+)', '$2 $1'

Note that the replacement pattern either needs to be in single quotes ('') or have the $ signs of the replacement group specifiers escaped ("`$2 `$1").

# may better replace with     $pattern = '(FROM) (?<replacement_place>[a-zA-Z0-9_.]{1,7})'$pattern = '(FROM [a-zA-Z0-9_.]{1,100})(?<replacement_place>[a-zA-Z0-9_.]{1,7})'Get-ChildItem -Path 'D:\Scripts' -Recurse -Include *.sql | % `{   (Get-Content $_.fullname) | % `     { $_-replace $pattern, '$1 replace text' } |      Set-Content $_.fullname -Force}

If you need to reference other variables in your replacement expression (as you may), you can use a double-quoted string and escape the capture dollars with a backtick

     { $_-replace $pattern, "`$1 replacement text with $somePoshVariable" } |