Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何用存储在数组中的数据替换方括号内的字符串部分?_C#_Arrays_Regex_Flee - Fatal编程技术网

C# 如何用存储在数组中的数据替换方括号内的字符串部分?

C# 如何用存储在数组中的数据替换方括号内的字符串部分?,c#,arrays,regex,flee,C#,Arrays,Regex,Flee,我创建了一个程序,它接受一个字符串(例如“[2*4]x+[3/2]x”),隔离方括号内有文本的所有实例,并将它们放在数组“matches”中。然后,它去掉方括号,通过某种函数(我使用的是函数库)获取每个字符串(例如2*4),并在将其存储到数组“answers”之前对其求值。我现在需要一种方法,用“answers”数组中的项替换原始字符串中方括号内的项,但我不知道如何做 public string result(string solution,int num1, int num2, int num

我创建了一个程序,它接受一个字符串(例如“[2*4]x+[3/2]x”),隔离方括号内有文本的所有实例,并将它们放在数组“matches”中。然后,它去掉方括号,通过某种函数(我使用的是函数库)获取每个字符串(例如2*4),并在将其存储到数组“answers”之前对其求值。我现在需要一种方法,用“answers”数组中的项替换原始字符串中方括号内的项,但我不知道如何做

public string result(string solution,int num1, int num2, int num3,int num4)
{ 
    Regex regex = new Regex(@"\[.*?\]");
    MatchCollection matches = regex.Matches(solution);
    int count = matches.Count;
    int [] answers = new int [10];
    for (int i = 0; i <= count; i++)
    {
        string match = matches[i].Value;
        match = match.Replace("[", "");
        match = match.Replace("]", "");
        Console.WriteLine(match);
        ExpressionOptions options = new ExpressionOptions();
        options.Imports.AddType(typeof(System.Math));
        ExpressionOwner owner = new ExpressionOwner();
        owner.a = num1;
        owner.b = num2;
        owner.c = num3;
        owner.d = num4;

        Expression expressionmethod = new Expression(match, owner, options);

        try
        {
            ExpressionEvaluator<int> evaluator = (ExpressionEvaluator<int>)expressionmethod.Evaluator;
            int result = evaluator();
            answers[i] = result;
        }
        catch
        {
            ExpressionEvaluator<double> evaluator = (ExpressionEvaluator<double>)expressionmethod.Evaluator;
            double result = evaluator();
            answers[i] = Convert.ToInt32(result);
        }
    }
}
公共字符串结果(字符串解决方案,int num1,int num2,int num3,int num4)
{ 
正则表达式正则表达式=新正则表达式(@“\[.*?\]”);
MatchCollection matches=regex.matches(解决方案);
int count=matches.count;
int[]答案=新的int[10];

对于(int i=0;i以及获取匹配项,使用具有相同模式的
regex.Split
获取匹配项之间的文本

然后,它只是一个用结果插值它们以构建结果字符串的例子

紧跟在
MatchCollection matches=regex.matches(解决方案);
添加行:

string[] otherStuff = regex.Split(solution);
Debug.Assert(otherStuff.Length == matches.Count + 1); // Optional obviously. Regex can be weird though, I'd check it just in case.
那你就做吧

StringBuilder finalResult = new StringBuilder();
finalResult.Append(otherStuff[0]);
for (int i = 0; i < count; i++)
{
    finalResult.Append(answers[i]);
    finalResult.Append(otherStuff[i+1]);
}
StringBuilder finalResult=new StringBuilder();
finalResult.Append(otherStuff[0]);
for(int i=0;i

finalResult
应该提供您所需的内容。

您可以使用
Regex。使用回调方法替换
作为替换参数,您可以对匹配值执行任何需要的操作,并在修改后将其放回结果字符串中。捕获方括号中的所有文本,以避免额外的操作与匹配值匹配

代码如下:

public string ReplaceCallback(Match m) 
{
    string match = m.Groups[1].Value;
    Console.WriteLine(match);
    ExpressionOptions options = new ExpressionOptions();
    options.Imports.AddType(typeof(System.Math));
    ExpressionOwner owner = new ExpressionOwner();
    owner.a = num1;
    owner.b = num2;
    owner.c = num3;
    owner.d = num4;

    Expression expressionmethod = new Expression(match, owner, options);

    try
    {
        ExpressionEvaluator<int> evaluator = (ExpressionEvaluator<int>)expressionmethod.Evaluator;
        int result = evaluator();
        return result.ToString();
    }
    catch
    {
        ExpressionEvaluator<double> evaluator = (ExpressionEvaluator<double>)expressionmethod.Evaluator;
        double result = evaluator();
        return result.ToString();
    }
}

public string result(string solution,int num1, int num2, int num3,int num4)
{
    return Regex.Replace(solution, @"\[(.*?)]", ReplaceCallback);
}
publicstringreplaceCallback(匹配m)
{
字符串匹配=m.Groups[1]。值;
控制台写入线(匹配);
ExpressionOptions=newexpressionoptions();
options.Imports.AddType(typeof(System.Math));
ExpressionOwner owner=新ExpressionOwner();
所有者a=num1;
所有者。b=num2;
业主:c=num3;
owner.d=num4;
表达式expressionmethod=新表达式(匹配、所有者、选项);
尝试
{
ExpressionEvaluator=(ExpressionEvaluator)expressionmethod.evaluator;
int result=evaluator();
返回result.ToString();
}
抓住
{
ExpressionEvaluator=(ExpressionEvaluator)expressionmethod.evaluator;
双重结果=评估器();
返回result.ToString();
}
}
公共字符串结果(字符串解决方案、int num1、int num2、int num3、int num4)
{
返回Regex.Replace(解决方案,@“\[(.*?)”,ReplaceCallback);
}

\[(.*?)
正则表达式匹配
[
,然后匹配并捕获除换行符以外的任何0+字符,然后匹配
]
字符。因此,
[…]
之间的文本在
匹配.Groups[1]内.Value
在回调方法中进一步修改。

提取匹配项时,可以在单独的数组中跟踪每个匹配项的开始和结束位置。这样,您就知道将计算值放回何处。或者,您可以将匹配项之间的内容保留在数组中。这样,您就不需要保存位置。part问题是我不知道如何把答案放回去,你能解释一下怎么做吗?非常感谢。你能解释一下我将如何做插值吗?我真的不知道这在这方面意味着什么。非常感谢,这似乎是一种方法,我很容易合并,并且理解,我会给出一个例子go:)我必须承认,我赞成Wiktor的答案,但这更接近您正在尝试的,可能更简单。很抱歉,我不熟悉回调方法的使用,快速的google让我更加困惑,您能解释一下它们是如何工作的吗?请看。重点是
ReplaceCallback
方法接受匹配对象这是在正则表达式中发现的,它返回修改后的字符串,将被
regex.Replace
替换。这肯定是一个比我更好的答案,但可能更高级。这并不是一件坏事,只是觉得有些人可能很难理解,我总是建议使用一个你可以理解的答案n即使不是最好的,也要理解-毕竟,你必须维护你编写的代码。@flyn1179使用带有正则表达式的回调函数。替换是这里最自然的解决方案,因为字符串只处理一次,没有必要使用正则表达式两次,也不用担心插入修改数据的位置。它不是“高级”的老实说,我同意你的观点,但是回调方法或函数指针的概念是初学者可能很难理解的。我自己并不认为它特别先进,但我已经编码了30年。我绝对认为它是值得理解和使用这个答案的,我从来不知道<代码>。>Regex.Replace以前也可以这样做,所以我也学到了一些东西!