none
Invalid RegEx Pattern RRS feed

  • Question

  • I'm trying to use -replace in PowerShell to match some text in a WinSCP ini file. I need to replace the following values with variables:

    $inifile -replace '[Sessions\ucs-backup@ip-address]', $session_ip -replace 'HostName=ip-address', $host_ip | Set-Content 'C:\WinSCP\WinSCP.ini'

    The two variables effectively let me configure the IP address without using the GUI but I'm not sure what I'm doing with the RegEx string. I assume I'm using invalid characters that need escaping but I'm not sure how? Any help would be appreciated.

    Thanks in advance
    Adam

    Wednesday, January 7, 2015 2:41 PM

Answers

  • Backslashes need to be escaped in regex and you can't have the dash by itself as it denotes a range. You could substitute it for \w or escape it \-

    $inifile -replace '[Sessions\\ucs\-backup@ip\-address]', $session_ip -replace ''HostName=ip\-address'', $host_ip | Set-Content 'C:\WinSCP\WinSCP.ini'
    $inifile -replace '[Sessions\\ucs\wbackup@ip\waddress]', $session_ip -replace ''HostName=ip\-address'', $host_ip | Set-Content 'C:\WinSCP\WinSCP.ini'





    • Marked as answer by Adam-M Wednesday, January 7, 2015 2:54 PM
    • Edited by Braham20 Wednesday, January 7, 2015 2:55 PM
    Wednesday, January 7, 2015 2:43 PM

All replies

  • Backslashes need to be escaped in regex and you can't have the dash by itself as it denotes a range. You could substitute it for \w or escape it \-

    $inifile -replace '[Sessions\\ucs\-backup@ip\-address]', $session_ip -replace ''HostName=ip\-address'', $host_ip | Set-Content 'C:\WinSCP\WinSCP.ini'
    $inifile -replace '[Sessions\\ucs\wbackup@ip\waddress]', $session_ip -replace ''HostName=ip\-address'', $host_ip | Set-Content 'C:\WinSCP\WinSCP.ini'





    • Marked as answer by Adam-M Wednesday, January 7, 2015 2:54 PM
    • Edited by Braham20 Wednesday, January 7, 2015 2:55 PM
    Wednesday, January 7, 2015 2:43 PM
  • Thanks - that worked great
    Wednesday, January 7, 2015 2:54 PM