0

在下面的这段代码中,我想用正则表达式返回这个字符串:' DataSource=xxxtransxxx;Initial Catalog=Sales'

Sales可以或不能有 aprefix并且在上面的字符串中的项目之间可以或不可以有空格。我在下面的代码中尝试了正则表达式,但它不起作用。任何建议表示赞赏,谢谢!

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 回答