一个数字前后的Unity C#字符串修改

问题描述 投票:-2回答:1

我正在制作计算器应用,但遇到了一些小麻烦:

例如,我使用该字符串:“ 58(-)+ 69 + 32(-)* 3”。

问题是:如何在C#中将前面的字符串更改为:“(-58)+69 +(-32)* 3”?

非常感谢您的宝贵时间!

regex string
1个回答
0
投票

也许,

(\d+)\(-\)

替换为,

(-$1)

会有点接近:

RegEx Demo

测试

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"(\d+)\(-\)";
        string substitution = @"(-$1)";
        string input = @"58(-)+69+32(-)*3
58(-)+69+32(-)*358(-)+69+32(-)*3

";
        RegexOptions options = RegexOptions.Multiline;

        Regex regex = new Regex(pattern, options);
        string result = regex.Replace(input, substitution);
    }
}

C# Demo


如果要简化/更新/探索表达式,请在regex101.com的右上角进行说明。如果您有兴趣,可以观看匹配的步骤或在this debugger link中进行修改。调试器演示了a RegEx engine如何逐步使用一些示例输入字符串并执行匹配过程。


RegEx电路

[jex.im可视化正则表达式:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.