2015-03-03 291 views
0

我有串正則表達式查找所有的搜索關鍵詞匹配

var statement="ROUND(CDBL('0'),2)=ROUND((CDBL('0.00')*CDBL('0')/100),2)"

,我有正則表達式

@"[ROUND^]+\(\w+\([\'^].*?[\']\)+[\,]+[\d]+\)|[ROUND^]+\(+[\']+.+[\']+[\,]+[\d]+\)" 
從字符串我想從我的正則表達式搜索象下面這樣全面功能

MatchCollection roundFunctions = Regex.Matches(statement, @"[ROUND^]+\(\w+\([\'^].*?[\']\)+[\,]+[\d]+\)|[ROUND^]+\(+[\']+.+[\']+[\,]+[\d]+\)"); 

從MatchCollection結果我只有一個匹配。我期待兩場比賽,因爲我的字符串有兩個ROUND函數。我的正則表達式中有沒有丟失?請幫忙。正則表達式我不擅長。

更新:

下面是在線正則表達式的截圖。正如你所看到的,只有第一個ROUND函數已經匹配。我也需要獲得第二個ROUND函數。

http://regexhero.net/tester/ http://regexhero.net/tester/

更新時間:

更清晰,我期待什麼輸出以下正則表達式。

[ROUND^]+\(\w+\([\'^].*?[\']\)+[\,]+[\d]+\)|[ROUND^]+\(+[\']+.+[\']+[\,]+[\d]+\) 

目前,它僅匹配以下字符串:

  1. ROUND(CDBL( '0'),2)
  2. ROUND(CDBL( '1'),2)+ ROUND(CDBL('3.36'),2)
  3. ROUND(CDBL('4.25'),3)* ROUND(CDBL('5.76' )/ ROUND(CDBL('10'),2)
  4. ROUND(CDBL('100 0.55 '),2)-Round(CDBL('10 2.2'),2)

我想這正則表達式是爲了匹配以下

  1. ROUND((CDBL('5.24。 00 ')* CDBL(' 7.4 ')/ 100),2)
  2. ROUND((CDBL(' 300.45.00 ') - CDBL(' 100.4' )/ 100),2)

請幫幫我。

+0

你需要一輪爭論?或者只是爲了統計ROUND呼叫的數量? – Paul 2015-03-03 04:05:37

+0

@Paul我需要參數爲round,因爲在匹配之後,我循環來自roundFunctions的ROUND匹配來計算每個方程。 – 2015-03-03 05:36:04

+0

你可以鏈接ROUND語句(ROUND(ROUND(...))嗎?如果是這樣,我建議你看看語法分析器,而不是簡單的正則表達式。例如http://goldparser.org/ - 他們有用於構建語法和.NET實現的工具來解析它。 – 2015-03-03 06:26:50

回答

2

我會推薦一個工具,如gplex/gppg,GOLD,ANTLR來做這種工作,特別是因爲你會評估解析結果。

如果你堅持使用.NET RegEx,我建議你看看使用Balancing Group Definitions

如果你想要一些更容易處理的事情,那麼我會考慮編寫一個簡單的Recursive Descent Parser將輸入分解爲語法樹,然後對其進行評估。維基百科有一個用C語言編寫的體面樣本(沒有指針,所以很容易轉換爲.net),我也在下面複製它。

均衡組樣品

using System; 
using System.Text.RegularExpressions; 

class Example 
{ 
    public static void Main() 
    { 
     string pattern = "^[^<>]*" + 
         "(" + 
         "((?'Open'<)[^<>]*)+" + 
         "((?'Close-Open'>)[^<>]*)+" + 
         ")*" + 
         "(?(Open)(?!))$"; 
     string input = "<abc><mno<xyz>>"; 

     Match m = Regex.Match(input, pattern); 
     if (m.Success == true) 
     { 
     Console.WriteLine("Input: \"{0}\" \nMatch: \"{1}\"", input, m); 
     int grpCtr = 0; 
     foreach (Group grp in m.Groups) 
     { 
      Console.WriteLine(" Group {0}: {1}", grpCtr, grp.Value); 
      grpCtr++; 
      int capCtr = 0; 
      foreach (Capture cap in grp.Captures) 
      {    
       Console.WriteLine("  Capture {0}: {1}", capCtr, cap.Value); 
       capCtr++; 
      } 
      } 
     } 
     else 
     { 
     Console.WriteLine("Match failed."); 
     } 
    } 
} 
// The example displays the following output: 
// Input: "<abc><mno<xyz>>" 
// Match: "<abc><mno<xyz>>" 
//  Group 0: <abc><mno<xyz>> 
//   Capture 0: <abc><mno<xyz>> 
//  Group 1: <mno<xyz>> 
//   Capture 0: <abc> 
//   Capture 1: <mno<xyz>> 
//  Group 2: <xyz 
//   Capture 0: <abc 
//   Capture 1: <mno 
//   Capture 2: <xyz 
//  Group 3: > 
//   Capture 0: > 
//   Capture 1: > 
//   Capture 2: > 
//  Group 4: 
//  Group 5: mno<xyz> 
//   Capture 0: abc 
//   Capture 1: xyz 
//   Capture 2: mno<xyz> 

遞歸下降解析器樣品(來自Wikipedia

typedef enum {ident, number, lparen, rparen, times, slash, plus, 
    minus, eql, neq, lss, leq, gtr, geq, callsym, beginsym, semicolon, 
    endsym, ifsym, whilesym, becomes, thensym, dosym, constsym, comma, 
    varsym, procsym, period, oddsym} Symbol; 

Symbol sym; 
void getsym(void); 
void error(const char msg[]); 
void expression(void); 

int accept(Symbol s) { 
    if (sym == s) { 
     getsym(); 
     return 1; 
    } 
    return 0; 
} 

int expect(Symbol s) { 
    if (accept(s)) 
     return 1; 
    error("expect: unexpected symbol"); 
    return 0; 
} 

void factor(void) { 
    if (accept(ident)) { 
     ; 
    } else if (accept(number)) { 
     ; 
    } else if (accept(lparen)) { 
     expression(); 
     expect(rparen); 
    } else { 
     error("factor: syntax error"); 
     getsym(); 
    } 
} 

void term(void) { 
    factor(); 
    while (sym == times || sym == slash) { 
     getsym(); 
     factor(); 
    } 
} 

void expression(void) { 
    if (sym == plus || sym == minus) 
     getsym(); 
    term(); 
    while (sym == plus || sym == minus) { 
     getsym(); 
     term(); 
    } 
} 

void condition(void) { 
    if (accept(oddsym)) { 
     expression(); 
    } else { 
     expression(); 
     if (sym == eql || sym == neq || sym == lss || sym == leq || sym == gtr || sym == geq) { 
      getsym(); 
      expression(); 
     } else { 
      error("condition: invalid operator"); 
      getsym(); 
     } 
    } 
} 

void statement(void) { 
    if (accept(ident)) { 
     expect(becomes); 
     expression(); 
    } else if (accept(callsym)) { 
     expect(ident); 
    } else if (accept(beginsym)) { 
     do { 
      statement(); 
     } while (accept(semicolon)); 
     expect(endsym); 
    } else if (accept(ifsym)) { 
     condition(); 
     expect(thensym); 
     statement(); 
    } else if (accept(whilesym)) { 
     condition(); 
     expect(dosym); 
     statement(); 
    } else { 
     error("statement: syntax error"); 
     getsym(); 
    } 
} 

void block(void) { 
    if (accept(constsym)) { 
     do { 
      expect(ident); 
      expect(eql); 
      expect(number); 
     } while (accept(comma)); 
     expect(semicolon); 
    } 
    if (accept(varsym)) { 
     do { 
      expect(ident); 
     } while (accept(comma)); 
     expect(semicolon); 
    } 
    while (accept(procsym)) { 
     expect(ident); 
     expect(semicolon); 
     block(); 
     expect(semicolon); 
    } 
    statement(); 
} 

void program(void) { 
    getsym(); 
    block(); 
    expect(period); 
}