2014-03-24 69 views
0

我想用正則表達式解析它變成組正則表達式解析字符串輸入

string input = @"(1,2)(3,4)"; 
Regex.Matches(input, @"\((\d,\d)\)"); 

結果我得到的不僅是1,2和3,4,而且空間。你們能幫我嗎?

編輯:

我想得到2組1,2和3,4。

+0

請張貼您獲得的輸出。 – NeverHopeless

+0

不適合我.....你在使用不同的輸入嗎? –

+0

同樣不適合我也 – Amitesh

回答

0

你如何接觸他們?嘗試:

實施例:

MatchCollection matchs = Regex.Matches(input, @"\((\d,\d)\)"); 
foreach (Match m in matchs) 
{ 
    rtb1.Text += "\n\n" + m.Captures[0].Value; 
} 
0

嘗試尋找在這個模式:

(\((?:\d,\d)\))+ 

+允許該基團被重複並且可能發生的一個或更多的時間。

+0

它不起作用 –

+0

向我們展示一些代碼,那就是如何從正則表達式匹配中獲得價值。 – NeverHopeless

1
string input = @"(1,2)(3,4)"; 
MatchCollection inputMatch= Regex.Matches(collegeRecord.ToString(), @"(?<=\().*?(?=\))"); 

對於當前字符串你會得到兩個輸出:

inputMatch[0].Groups[0].Value; 
inputMatch[0].Groups[1].Value; 

或者

您也可以嘗試foreach循環

foreach (Match match in inputMatch) 
{ 

} 

我沒有測試此代碼,

我的工作E xample:

MatchCollection facilities = Regex.Matches(collegeRecord.ToString(), @"<td width=""38"">(.*?)image_tooltip"); 
      foreach (Match facility in facilities) 
      { 
       collegeDetailDH.InsertFacilityDetails(collegeDetailDH._CollegeID, facility.ToString().Replace("<td width=\"38\">", string.Empty).Replace("<span class=\"icon_", string.Empty).Replace("image_tooltip", string.Empty)); 
      } 
0

您需要使用lookarounds。

string input = @"(1,2)(3,4)"; 
foreach (Match match in Regex.Matches(input, @"(?<=\().*?(?=\))")) 
    Console.WriteLine(match.Value); 

如果字符串可能有其他內容,那麼數字在括號中,你只需要那些具有數字裏面,你可以按如下方式使用更具體的正則表達式。

string input = @"(1,2)(3,4)"; 
foreach (Match match in Regex.Matches(input, @"(?<=\()\d,\d(?=\))")) 
    Console.WriteLine(match.Value);