2012-01-11 205 views
-2

環境:Visual Studio 2008 SP1正則表達式從字符串獲取整數

在這種情況下,在括號100之間得到整數的正則表達式是什麼?

string input = "bunch of text(100): more text here ignore the 1 or 2 ints here"; 
+0

只是真正的新正則表達式和掙扎很少得到答案。 – Rod 2012-01-11 02:04:05

+0

對不起所有的更新OP – Rod 2012-01-11 02:07:03

回答

2

這將是:

\d+ 

C#代碼(需要using System.Text.RegularExpressions,當然):

Regex re = new Regex(@"\d+"); 
string input = "bunch of text(100): more text here"; 
Match m = re.Match(input); 

if(m.Success) { 
    // Your number is in m.Value 
} else { 
    // No number in the string 
} 
+0

你如何確定它在開括號和右括號之間得到整數 – Rod 2012-01-11 01:58:44

+0

@rod:使用正則表達式:'(?<= \()\ d +(?= \))' – Ryan 2012-01-11 02:07:14