0

以下のこのコードでは、正規表現を訴えるこの文字列を返したいと思います: ' DataSource=xxxtransxxx;Initial Catalog=Sales'

Salesprefix上記の文字列の項目間にスペースを入れることも入れることもできません。以下のコードで正規表現を試しましたが、うまくいきません。アドバイスをいただければ幸いです、ありがとう!!

var thestring = @"'SELECT CONVERT(VARCHAR(MAX), [Measures].[EmployeeID ParameterCaption]) AS [EmployeeID]
                            ,CONVERT(VARCHAR(50),  [Measures].[Sales Rep Name ParameterCaption]) AS [EmployeeName]
                            ,CONVERT(VARCHAR(50),  [Measures].[Manager EmployeeID ParameterCaption]) AS   [ManagerEmployeeID]
                            ,CONVERT(MONEY, [MEASURES].[PrevFYYTD])AS  PrevFYYTD
                            ,CONVERT(MONEY, [MEASURES].[CurrentFYYTD] ) AS CurrentFYYTD 
                            ,CONVERT(VARCHAR(50),[MEASURES].[PCTGrowth] )+''%'' AS [PCTGrowth]
                            ,CONVERT(VARCHAR, [MEASURES].[DollarGrowth] ) AS DollarGrowth 
                            ,CONVERT(VARCHAR, [MEASURES].[HasParent] )  AS HasParent
                            ,CONVERT(VARCHAR, [MEASURES].[HasChild] ) AS HasChild 
                       FROM OPENROWSET(''MSOLAP'',''DataSource=xxxtransxxx;Initial Catalog=Sales''  , SET @MDX =     ''' WITH;";

        Regex rgx = new Regex(@"'\s*DataSource\s*=\s.*trans*(.*Sales) *'", RegexOptions.IgnoreCase);
        string result = rgx.Match(thestring).Groups[0].Value;
4

1 に答える 1

1

あなたが使用することができます

\s*DataSource\s*=[^';]+?;\s*Initial *Catalog\s*=[^;']+$

コード

string resultString = null;
try {
    resultString = Regex.Match(subjectString, @"\bDataSource\s*=[^';]+?;\s*Initial *Catalog\s*=[^;']+", RegexOptions.IgnoreCase | RegexOptions.Multiline).Value;
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}

説明

@"
(?i)          # Match the remainder of the regex with the options: case insensitive (i)
\b            # Assert position at a word boundary
DataSource    # Match the characters “DataSource” literally
\s            # Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
   *             # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
=             # Match the character “=” literally
[^';]         # Match a single character NOT present in the list “';”
   +?            # Between one and unlimited times, as few times as possible, expanding as needed (lazy)
;             # Match the character “;” literally
\s            # Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
   *             # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Initial       # Match the characters “Initial” literally
\             # Match the character “ ” literally
   *             # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Catalog       # Match the characters “Catalog” literally
\s            # Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
   *             # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
=             # Match the character “=” literally
[^;']         # Match a single character NOT present in the list “;'”
   +             # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
"
于 2012-06-06T04:30:43.933 に答える