2

我有以下代码

string s = "add(2,3);";
var matchess = Regex.Matches(s, @"\(\s*(?<num>\d+)\s*(\,\s*(?<num>\d+)\s*)*\)");
                var results = matchess.Cast<Match>()
                    .SelectMany(m => m.Groups["num"].Captures.Cast<Capture>())
                    .Select(c => c.Value).ToArray();

上面的代码检测到超过 2 个 functionname(number,number) 的输入,并将结果存储在 arraylist 中以便能够访问。

有没有办法改变方程,使其也接受以下内容并相应地打印结果?

示例 1

 string s = "add(2.2,3.3);";

结果 1

results[0] = 2.2
results[1] = 3.3

示例 2

string s = "add(2.4,3.5,6.7);";

结果 2

    results[0] = 2.4
    results[1] = 3.5
    results[2] = 6.7

示例 3

string s = "test("this is a test","just an example");";

结果 3

results[0] = "this is a test"
results[1] = "just an example"

示例 4

 string s = "test("this is a test","just an example","testing");";

结果 4

    results[0] = "this is a test"
    results[1] = "just an example"
    results[2] = "testing"

基本上我需要的是函数名(十进制,十进制)和函数名(字符串,字符串)的2个以上输入并将结果存储在数组列表中以访问它们。

请指教。谢谢

4

1 回答 1

1

试试这个:

string s = "aa(1.2,3.5)";
    //Or
    string s = "aa(sad,asd)";
    var info = Regex.Split(
        Regex.Matches(s, @"\(.*?\)")
        .Cast<Match>().First().ToString()
        .Replace("(", string.Empty).
        Replace(")", string.Empty), @"[\s,]+");

它是如何工作的:

首先我在区域得到字符串()

Regex.Matches(s, @"\(.*?\)");

获得第一场比赛

.Cast<Match>().First().ToString()

删除()

.Replace("(", string.Empty).Replace(")", string.Empty)

,在表单结果之间拆分文本或数字

Regex.Split(result, @"[\s,]+");
于 2013-04-08T07:04:50.990 回答