正则表达式从字符串中获取整数

时间:2012-01-11 01:44:22

标签: c#

环境:Visual Studio 2008 SP1

在这种情况下,在括号之间得到整数的正则表达式是什么?

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

2 个答案:

答案 0 :(得分:2)

[0-9] +(?:\。[0-9] *)?

下次访问此网站:http://gskinner.com/RegExr/

答案 1 :(得分: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
}