我想爲下面的字符串使用正則表達式。示例字符串的正則表達式字符串
string: Some() Text (1)
I want to capture 'Some() Text' and '1'
string: Any() Text
I want to capture 'Any() Text' and '0'
我想出了下面的正則表達式來捕捉「文本」和「計數」,但它不符合上述第2前。
@"(?<text>.+)\((?<count>\d+)\)
C#:
string pattern = @"(?<text>.+)\((?<count>\d+)\)";
Match m = Regex.Match(line, pattern);
count = 0;
text = "";
if (m.Success)
{
text = m.Groups["text"].Value.Trim();
int.TryParse(m.Groups["count"].Value, out count);
}
不起作用。 。+將爲所有第一名前鋒奪冠。 – hIpPy 2012-03-05 23:15:58
@hIpPy:現在修復。 – Ryan 2012-03-05 23:23:35
minitech,仍然不起作用。 – hIpPy 2012-03-05 23:31:54