regexpowershelltnsnames

How to write a regular expression to replace only the HOST name in the tnsnames.ora using powershell


(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ora)))

I want to replace the HOST name with a new value using powershell, Thank you


Solution

  • Use the -replace operator:

    $str = '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ora)))'
    
    $str -replace '(?<=\(HOST = )[^)]+', 'newhost'