Powershell and regex using Select-String

Hello I am trying to extract the servername and database name from a file that contains a connection string. The code I am using is below. The first line can extract the servername fine but the second line cannot, it only shows a blank. What I am missing please help.

Select-String -Path C:\Tmp\movie*.properties -Pattern '(?<=sqlserver://).*?(?=;)' -AllMatches | % { $_.Matches } | % { $_.Value } #- simplematch
Select-String -Path C:\Tmp\movie*.properties -Pattern '(?<=databaseName).*?(?=username)' -AllMatches #| % { $_.Matches } | % { $_.Value } #- simplematch

the .properties files has contents similar to the one below.

driver=net.sourceforge.jtds.jdbc.Driver
url=jdbc:jtds:sqlserver://netflix;appName=Migrate;databaseName=movie_hitsusername=adminpassword=moviecritic#

April 29th, 2015 3:33pm

Sorry but those file contents are either damaged or have been altered.  Try to find a known good file and be careful not to alter it.

Free Windows Admin Tool Kit Click here and download it now
April 29th, 2015 4:05pm

Hi Kakefiled,

I tested the script regardless of the file format, and I got the desired value of the severname and database name:

$a="url=jdbc:jtds:sqlserver://netflix;appName=Migrate;databaseName=movie_hitsusername=adminpassword=moviecritic#"

$a|Select-String -Pattern '(?<=sqlserver://).*?(?=;)' -AllMatches | % { $_.Matches } | % { $_.Value }
 
$a| Select-String -Pattern '(?<=databaseName=).*?(?=username)' -AllMatches | % { $_.Matches } | % { $_.Value } 

The result:

If there is anything else regarding this issue, please feel free to post back.

Best Regards,

Anna Wang

April 30th, 2015 6:37am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics