我无法捕获字符串中的值。我只想要我不想捕获的数字T
或:
。这个失败的测试解释为:正则表达式捕获
[TestMethod]
public void RegExTest()
{
var rex = new Regex("^T([0-9]+):"); //as far as I understand, the() denote the capture group
var match = rex.Match("T12:abc");
Assert.IsTrue(match.Success);
Assert.IsTrue(match.Groups[0].Success);
Assert.AreEqual("12", match.Groups[0].Captures[0]); //fails, actual is "T12:"
}
不管你不想捕捉你必须表示它。你的正则表达式应该是'^(?: T)([0-9] +)(?:\:)$' –
@AndreCalil我明白了,是的,除了必须使用Groups [1]结果。谢谢。 – weston
第一组是组[1]。组[0]是整个比赛。 – Toto