So basically you need to match sequences of numbers that are not preceded or followed by a decimal point or another digit? Why not try just that?
[TestCase("'((1/1000)*2375.50)'", new string[] { "1", "1000" })] [TestCase("1", new string[] { "1" })] [TestCase("1 2", new string[] { "1", "2" })] [TestCase("123 345", new string[] { "123", "345" })] [TestCase("123 3.5 345", new string[] { "123", "345" })] [TestCase("123 3. 345", new string[] { "123", "345" })] [TestCase("123 .5 345", new string[] { "123", "345" })] [TestCase(".5-1", new string[] { "-1" })] [TestCase("0.5-1", new string[] { "-1" })] [TestCase("3.-1", new string[] { "-1" })] public void Regex(string input, string[] expected) { Regex regex = new Regex(@"(?:(?<![.\d])|-)\d+(?![.\d])"); Assert.That(regex.Matches(input) .Cast<Match>() .Select(m => m.ToString()) .ToArray(), Is.EqualTo(expected)); }
Seems to work.
source share