-
May 9th, 2024, 12:05 PM
#1
SQL Connection string regex pattern to parse sections
Hey all I am in need of getting a regex so that i can parse a sql connection string out.
I am currently doing so with .Split() but that is proving to not be very reliable.
The connection string looks like this:
dbConnectionString=jdbc:sqlserver://theServerName:1433;database=theDBName;intergratedSecurity=true;encrypt=true;trustServerCertificate=true
The .Split() code I am currently using is this (data being the connection string above):
Code:
string[] values = data.Split('=');
switch ((entry.Key.ToLower()))
{
case "dbconnectionstring":
string[] __values = data.Split(':');
string[] ___values = __values[3].Split(';');
txt_box1 = __value[0].ToLower().Replace("dbConnectionString", "") + ":" + __values[1].ToLower();
txt_box2 = __values[2].Replace("//", "");
txt_box3 = ___values[0];
break;
case "database":
txt_box4 = _values[1];
break;
case "encrypt":
txt_box5 = _values[1];
break;
case "trustServerCertificate":
txt_box6 = _values[1];
break;
}
It gets messed up between the splits of ":" and ";" so this is why I would like to figure out a regex pattern so its much easier (and more reliable) than doing this .Split() stuff. I'm just not really good at regex!
What I am needing to get is the format:
Code:
jdbc:sqlserver://
theServerName
1433
theDBName
true
true
true
And help would be great!
-
May 9th, 2024, 02:31 PM
#2
Re: SQL Connection string regex pattern to parse sections
The top-level split should be by semi-colon to yield some name=value pairs.
So
dbConnectionString=jdbc:sqlserver://theServerName:1433
database=theDBName
intergratedSecurity=true
encrypt=true
trustServerCertificate=true
Then you split each one at '='.
-
May 9th, 2024, 02:58 PM
#3
Re: SQL Connection string regex pattern to parse sections
 Originally Posted by StealthRT
Hey all I am in need of getting a regex so that i can parse a sql connection string out.
... help would be great!
https://www.google.com/search?q=conn...t=gws-wiz-serp
Victor Nijegorodov
-
May 9th, 2024, 03:32 PM
#4
Re: SQL Connection string regex pattern to parse sections
Thanks for your help! Got it.
-
May 14th, 2024, 05:35 AM
#5
Re: SQL Connection string regex pattern to parse sections
For contrast (/illustrative purposes), a basic (.NET) RegEx:
Code:
dbConnectionString=(.*://)(.*):(.*);database=(.*);intergratedSecurity=(.*);encrypt=(.*);trustServerCertificate=(.*)
The same but with the (capture) 'groups' named:
Code:
dbConnectionString=(?<dbConnType>.*://)(?<svrName>.*):(?<portNum>.*);database=(?<dbName>.*);intergratedSecurity=(?<intSec>.*);encrypt=(?<encrypt>.*);trustServerCertificate=(?<trustSvrCert>.*)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|