Reputation: 17
In this string:
"Data Source=srv1-a.be.com;Initial Catalog=db1;Provider=SQLNCLI11;Integrated Security=SSPI;"
I would like to replace the data source name by localhost whatever the data source name, with PowerShell script.
With the following code the string is truncated after localhost!
I get the result → Data Source=localhost
.
"Data Source=srv1-a.be.com;Initial Catalog=db1;Provider=SQLNCLI11;Integrated Security=SSPI;" -replace '(?<grp1>Data Source=)(.*)', '${grp1}localhost'
The result should be:
"Data Source=localhost;Initial Catalog=db1;Provider=SQLNCLI11;Integrated Security=SSPI;"
Upvotes: 0
Views: 97
Reputation: 25001
I modified your -Replace
operation slightly:
"Data Source=srv1-a.be.com;Initial Catalog=db1;Provider=SQLNCLI11;Integrated Security=SSPI;" -replace '(?<=Data Source=)[^;]+','localhost'
I am using a positive lookbehind (?<=)
to find the Data Source=
string and set the position at that location (character just after the =
). [^;]+
matches one or more consecutive characters that are not ;
from the current position. Then we are just simply replacing the matched string with localhost
. As a result, only the server name is matched and updated.
Upvotes: 0