2013-03-23 18 views
0

您好我有下面的代碼行正則表達式得到以下模式

{ 
.....(here i can have anything, numbers, string special characters 
...."hello" 
} 

我需要知道哪些發現上面的圖案,我正則表達式模式,其中串從{然後一些文本,然後一個關鍵字開始像「你好」然後}。

請幫

,所以我不得不

function test(a,b,c){ 
} 

function test2(c,a,d){ 
if(a > 0){ 
    alert('test'); 
} 

和我得到這個表達function\s+(\S+)\s*\((.|\n)*?\)\s*{

這給了我function test(a,b,c)

function test2(c,a,d) 

我希望有一個正則表達式,給了我當它找到一個像'測試「內部的功能。

有意義嗎?

+1

你能更具體,並張貼應該被解析一個或兩個以上的例子嗎? – Appleshell 2013-03-23 12:33:45

+0

好吧讓我發帖 – okay 2013-03-23 12:38:01

+0

現在沒有任何意義? – okay 2013-03-23 12:42:24

回答

1

我不知道C#,但像(與.匹配換行)正則表達式:

.*\bfunction\b.+?\bhello\b.+?\} 

應該做的工作。

說明:

The regular expression: 
(?s-imx:.*\bfunction\b.+?\bhello\b.+?\}) 
matches as follows: 

NODE      EXPLANATION 
---------------------------------------------------------------------- 
(?s-imx:     group, but do not capture (with . matching 
         \n) (case-sensitive) (with^and $ matching 
         normally) (matching whitespace and # 
         normally): 
---------------------------------------------------------------------- 
    .*      any character (0 or more times (matching 
          the most amount possible)) 
---------------------------------------------------------------------- 
    \b      the boundary between a word char (\w) and 
          something that is not a word char 
---------------------------------------------------------------------- 
    function     'function' 
---------------------------------------------------------------------- 
    \b      the boundary between a word char (\w) and 
          something that is not a word char 
---------------------------------------------------------------------- 
    .+?      any character (1 or more times (matching 
          the least amount possible)) 
---------------------------------------------------------------------- 
    \b      the boundary between a word char (\w) and 
          something that is not a word char 
---------------------------------------------------------------------- 
    hello     'hello' 
---------------------------------------------------------------------- 
    \b      the boundary between a word char (\w) and 
          something that is not a word char 
---------------------------------------------------------------------- 
    .+?      any character (1 or more times (matching 
          the least amount possible)) 
---------------------------------------------------------------------- 
    \}      '}' 
---------------------------------------------------------------------- 
)      end of grouping 
---------------------------------------------------------------------- 
+0

'function a(){「nothing here」};函數b(){「hello」}'會使你的正則表達式失敗。 – nhahtdh 2013-03-23 12:51:55

+0

這對我有效......謝謝 – okay 2013-03-23 13:02:01

+0

@nhahtdh:你說得對,正確。 – Toto 2013-03-23 13:24:35

0

如何:

MatchCollection matches = Regex.Matches(YourText, @"\{[^}]*""hello"".\}", RegexOptions.Multiline); 
foreach (Match match in matches) 
{ 
    foreach (Capture capture in match.Captures) 
    { 
    Console.WriteLine(capture.Value); 
} 
+0

我想你的意思是'單線',而不是'多線',這取決於你爲什麼把它包括在首位。 – 2013-03-23 13:03:24