CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2008
    Posts
    114

    Question 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!

  2. #2
    Join Date
    Nov 2018
    Posts
    154

    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 '='.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,427

    Re: SQL Connection string regex pattern to parse sections

    Quote Originally Posted by StealthRT View Post
    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

  4. #4
    Join Date
    Aug 2008
    Posts
    114

    Re: SQL Connection string regex pattern to parse sections

    Thanks for your help! Got it.

  5. #5
    Join Date
    May 2024
    Posts
    4

    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
  •  





Click Here to Expand Forum to Full Width

Featured