2011-12-15 42 views
-2

我需要一個正則表達式,可以從下面的文字中進行選擇:拒絕線,時間

string test hello world! 
    bitmap player player.png 
terrain(test) 
bg(sky) 
label(asdasd,sd, sd,ad,adsad, ds){sdds} 
00:30 test(asda,asdad,adsd)asdad{asd} 
    02:30 test(asda,asdad,adsd)asdad 
00:40 test(asda,asdad,adsd)asdad 

返回以下組:

{ 
"string test hello world!", 
"bitmap player player.png", 
"terrain(test)", 
"bg(sky)", 
"label(asdasd,sd, sd,ad,adsad, ds){sdds}" 
} 

我想用..:..避免的時間。

非常感謝。

我試圖

(?<!\b..:..\s).* 

,但沒有工作。

+0

我會逐行讀取輸入內容,然後可以丟棄符合我們標準的輸入內容;即:`^ \ s * \ d \ d:\ d \ d` – mpen 2011-12-15 07:30:08

回答

1

this使用(具有多行標誌):

^(?!\s*[0-9]{2}\:[0-9]{2})\s*(?<captured>.+)$ 
1

所以..你想要任何行不是以數字開頭?您的原始問題的標準不甚清楚。

你可以嘗試:

^ *(?![0-9 ])(.+?) *$ 

含義,「行,後跟空間的開始,接着是參選是一個數字或空格,空格結束」。

+0

任何不以##開頭的行:##(空格) – luis 2011-12-15 07:15:41

0

嘗試此,我用另外RegexOptions.IgnorePatternWhitespace允許可讀正則表達式和在正則表達式的評論,以及。

String s = @"string test hello world! 
    bitmap player player.png 
terrain(test) 
bg(sky) 
label(asdasd,sd, sd,ad,adsad, ds){sdds} 
00:30 test(asda,asdad,adsd)asdad{asd} 
    02:30 test(asda,asdad,adsd)asdad 
00:40 test(asda,asdad,adsd)asdad"; 

MatchCollection result = Regex.Matches 
    (s, @"^     # Match the start of the row (because of the Multiline option) 
      ?!\s*\d{2}:\d{2}) # Row should not start with \d{2}:\d{2} 
      \s*(.*)   # Match the row 
      $"     // Till the end of the row (because of the Multiline option) 
      ,RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace); 

foreach (Match item in result) { 
    Console.WriteLine(item.Groups[1]); 
} 
Console.ReadLine();