问题描述:

I need to extract file extension that is embedded in a connection string. I was wondering if it's possible to achieve it using Regex? or any solution would be of great help. The connection string looks something similar to this:

"Provider=MSDASQL.1;Persist Security Info=False;Extended

Properties="DSN=Excel Files; DBQ=<Assume_Location>FILE_NAME.xls;

DriverId=1046;MaxBufferSize=2048;PageTimeout=5;""

In this example the filename is of extension ".xls" but this can be anything e.g. xlsx, dbf, mdb, accdb etc. . We don't have control over how the connection string is generated

Data Source (i.e. DBQ in this example) may be different for different connection strings e.g. Network Address, SourceDB, Server, Hostname etc.

网友答案:

This will work assuming:

  • The file name does not contain ;
  • There is a period between the filename and the extension
  • There is a ; before the filename
  • There is a ; after the extension

    string pattern = @";(?<filename>[^;]*)[.]{1,1}.*;";
    string fileName = Regex.Match(connectionString, pattern).Groups["filename"].Value;
    

Or if this <Assume_Location> is before the file name rather than ; then you can use this for the pattern:

string pattern = @"<Assume_Location>(?<filename>[^;]*)[.]{1,1}.*;";

Edit 1 - In response to comment.

One of these should work if you need the extension as well:

string pattern = @";(?<filename>[^;]*[.]{1,1}[^;]*);";
string pattern = @"<Assume_Location>(?<filename>[^;]*[.]{1,1}[^;]*);";

Edit 2 - In response to more comments.

This should absolutely work (unless it does not, then I will edit this to tone down the confidence level).

string pattern = @";[^=]*=<[^>]*>(?<filename>[^;]*[.][^;]*);";

or this if &gt; and &lt; is not replaced with angle brackets:

string pattern = @";[^=]*=&lt;.*&gt;(?<filename>[^;]*[.][^;]*);";
网友答案:

Well, I am not 100% sure, but it seems like the file is the only thing that is not assigned, is it? Then you can use the following (not tested):

(;|^)(?<File>[^=]+)(;|$)
网友答案:

If the filename always starts with <Assume_Location> then you can:

public static string GetAssumeLocation(string connectionString)
{
    const string assumeLocation = "<Assume_Location>";

    var builder1 = new DbConnectionStringBuilder();
    builder1.ConnectionString = connectionString;
    object extended;

    if (builder1.TryGetValue("Extended Properties", out extended) && extended is string)
    {
        var builder2 = new DbConnectionStringBuilder();
        builder2.ConnectionString = (string)extended;

        foreach (KeyValuePair<string, object> kv in builder2)
        {
            var value = kv.Value as string;

            if (value != null && value.StartsWith(assumeLocation))
            {
                return Path.GetExtension(value.Substring(assumeLocation.Length));
            }
        }
    }

    return null;
}

Note that I'm using the DbConnectionStringBuilder to "split" the connection string, and I'm looking only in the Extended Properties.

相关阅读:
Top