2012-06-06 2 views
0

'DataSource=xxxtransxxx;Initial Catalog=Sales'절차 스크립트 텍스트의 정규식을 사용하여 문자열을 추출 하시겠습니까? 나는이 문자열 고소 정규식을 반환하고 싶습니다 아래이 코드에서

Sales는 또는 prefix이 없습니다 수 있으며, 이상 문자열의 항목 사이에 공백이있을 수 없습니다 수 있습니다. 아래 코드에서 정규식을 시도했지만 작동하지 않습니다. 모든 조언 감사, 감사합니다 !!

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; 
+0

프로그래밍 언어 용 태그를 추가하십시오 – Bergi

답변

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) 
" 
,
관련 문제