What am I doing wrong? I have the () to set up two Groups, and I expect sExtractNumber to create Groups with values of “0.234” and “”, but they both have .Value = “0.234”?
const string REGEX_NUMBER = "[0-9\\.-]*";
static readonly Regex sExtractNumber = new
Regex(string.Format("^({0})(.*)$", REGEX_NUMBER),
RegexOptions.Singleline | RegexOptions.Compiled);
–
[Test] public void ParseNumber()
{
double num;
string rest;
Assert.True(KbParser.ExtractNumber("0.234", out num, out rest));
Assert.AreEqual(0.234, num, 0.0001);
Assert.AreEqual(rest, "in"); // fails . Rest == "0.234"
Assert.True(KbParser.ExtractNumber("0.234in", out num, out rest));
Assert.AreEqual(0.234, num, 0.0001);
Assert.AreEqual(rest, "in");
}
–
public static bool ExtractNumber(string name, out double number, out string rest)
{
Match m = sExtractNumber.Match(name);
string numbertext = m.Groups[0].Value;
rest = m.Groups[1].Value;
return double.TryParse(numbertext, out number);
}
Groups[0]always contains the text matched. The first captured group is inGroups[1]. Therefore, yourExtractNumbermethod should be: