Parsing regular expressions coming out of GetSchema's Common Schema Collection

五迷三道 提交于 2020-01-05 07:53:05

问题


I have an unknown connection being passed in, through the base DbConnection, it could be MySQL, SQLite, Oracle, SQL Server, Access, I don't know.

I need to pull the quoted identifiers out of the schema information, I've figured out how to get the quoted identifiers, but it comes back in a RegEx, and I'm not familiar enough with RegEx to be able to parse these dynamically.

Given the C# code:

public class DatabaseHelper
{
    internal char LeftIdentifier { get; private set; }

    internal char RightIdentifier { get; private set; }

    private void SetQuotedIdentifiers(DbConnection connection)
    {
        string quotedIdentifier = connection.GetSchema("DataSourceInformation").Rows[0]["QuotedIdentifierPattern"].ToString();
        LeftIdentifier = null; // some code I have to figure out
        RightIdentifier = null; // some code I have to figure out
    }
}

I need help setting the values of LeftIdentifier, and RightIdentifier.

If I run that code, with an Oracle connection, quotedIdentifier comes back with the following result:

(([^\"]\"\")*)

I want to set LeftIdentifier in this case equal to " and RightIdentifier also to "

If I run that code, with a SQL Server connection, quotedIdentifier comes back with the following result:

(([^\\[]|\\]\\])*)

In this case, I want to set LeftIdentifier to [ and RightIdentifier equal to ]

Can anyone help with the code needed for this? I'm hitting a stumbling block with everything I'm trying.

Note: I got the information for the function from this MSDN article.


回答1:


Turn on Ignore Pattern whitespace for this pattern. Here I have escaped the identifiers as the hex values.

string pattern = #"
# \x28 (
# \x29 )
# \x22 "
# \x5B [
# \x5D ]

(?:[\x28\x29\x22\x5B\x5D])        # match but don't caputure the set above
(?<Text>[^\x28\x29\x22\x5B\x5D]+) # Get the inbetween text into Text capture group
(?:[\x28\x29\x22\x5B\x5D])        # match but don't caputure the set above    
";

or as a single line without the ignore

string pattern = @"(?:[\x28\x29\x22\x5B\x5D])(?<Text>[^\x28\x29\x22\x5B\x5D]+)(?:[\x28\x29\x22\x5B\x5D])";

Will match

"abc" [abc] (ab)

and returns the text abc in the named capture group named Text.



来源:https://stackoverflow.com/questions/8749959/parsing-regular-expressions-coming-out-of-getschemas-common-schema-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!