如何使用 powershell 编写正则表达式以仅替换 tnsnames.ora 中的主机名
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)))
我想使用 powershell 将 HOST 名称替换为新值,谢谢
使用运算符:
$str = '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ora)))'
$str -replace '(?<=\(HOST = )[^)]+', 'newhost'
它使用与 mklement0
相同的 -replace
运算符,但为像我这样的正则表达式挑战的人使用更简单的模式。 [咧嘴一笑]
$OldHost = 'myhost'
$NewHost = 'NewHostName'
$InStuff = '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ora)))'
$OutStuff = $InStuff -replace "HOST = $OldHost", "HOST = $NewHost"
$InStuff
$OutStuff
输出...
(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ora)))
(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = NewHostName)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ora)))
(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ora)))
我想使用 powershell 将 HOST 名称替换为新值,谢谢
使用
$str = '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ora)))'
$str -replace '(?<=\(HOST = )[^)]+', 'newhost'
它使用与 mklement0
相同的 -replace
运算符,但为像我这样的正则表达式挑战的人使用更简单的模式。 [咧嘴一笑]
$OldHost = 'myhost'
$NewHost = 'NewHostName'
$InStuff = '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ora)))'
$OutStuff = $InStuff -replace "HOST = $OldHost", "HOST = $NewHost"
$InStuff
$OutStuff
输出...
(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ora)))
(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = NewHostName)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ora)))