所以基本上你需要匹配不受小數點或其它數字之前或之後的數字序列?爲什麼不試試這個?
[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));
}
似乎工作。
所有輸入字符串的格式爲'((x/y)* z)'? – Chrono
@Chrono:不,這只是一個示例字符串。任何字符串都會做 –