简体   繁体   English

用特殊字符替换所有连续出现的字符

[英]Replace all consecutive occurrences of a char with special chars

I have fill in the blanks form where the length of each blank is not consistent I want to replace such banks with special code to align with business logic, below is how the formats are 我填写空白表单,其中每个空白的长度不一致我想用特殊代码替换这些库以符合业务逻辑,下面是格式是如何

It is raining ________ in the forest   
The quick _______ dog jumps over ______________ fox

I want to reformat above lines as below 我想重新格式化以上行,如下所示

It is raining [0] in the forest
The quick [0] dog jumps over [1] fox

As said the char length of each blank is not consistent, keeping that as case want to write a maintainable code in c# using either regular expression or without 如上所述,每个空白的字符长度不一致,保持该情况想要在c#中使用正则表达式或不使用正则表达式编写可维护代码

EXCEPTIONS 例外

There are some entries without any blanks in which case should return the whole passage as is 有些条目没有任何空格,在这种情况下应该按原样返回整个段落

The code suggested by Tim works great my code with Tim's answer is like below. 蒂姆建议的代码很好,我的代码与蒂姆的答案如下。 Hope may help someone 希望可以帮助别人

    Dictionary<string, string> dctE = new Dictionary<string, string>();
    Dictionary<string, string> dctT = new Dictionary<string, string>();
    string jsonT = string.Empty, jsonH = string.Empty;
    try
    {
        using (StreamReader r = new StreamReader(@"C:\samples\testmongo\testmongo\tamil1.txt"))
        {
            string langs = r.ReadToEnd();
            var lines = langs.Split('\n');
            for (int i = 1; i < lines.Length - 2; i += 2)
            {
                string tml = lines[i].Split(':')[1];
                    Regex regex = new Regex(@"_{2,}");
                    string[] partT = regex.Split(tml);
                    for (int j = 0; j < partT.Length; j++)
                    {
                        tml += partT[j] + "[" + j + "]";
                    }
                //dctE[lines[i].Split(':')[0].Trim()] = lines[i].Split(':')[1].Trim();
                dctT[lines[i - 1].Split(':')[0].Trim()] = tml;// lines[i].Split(':')[1].Trim();
            }

        }
        jsonT = JsonConvert.SerializeObject(dctT);

    }
    catch(Exception eX)
    {
        Console.WriteLine(eX.Message);
    }
    finally
    {
        System.IO.File.WriteAllText(@"C:\samples\testmongo\testmongo\ta_yogs.json", jsonT);
        dctE.Clear();
        dctT.Clear();
    }

Here is a working script. 这是一个工作脚本。 This approach is to split the input string on two or more underscores ( _{2,} ). 此方法是将输入字符串拆分为两个或多个下划线( _{2,} )。 Then, we iterate the string components, and join them together into a single string using a numbered for loop, using which we can figure out what the replacement placeholders should be. 然后,我们迭代字符串组件,并使用编号的for循环将它们连接在一起成为一个字符串,使用它我们可以找出替换占位符应该是什么。

string input = "The quick _______ dog jumps over ______________ fox";
Regex regex = new Regex(@"_{2,}");
string[] parts = regex.Split(input);
string output = "";
for (int i=0; i < parts.Length-1; i++)
{
    output += parts[i] + "[" + i + "]";
}
output += parts[parts.Length-1];
Console.WriteLine(input);
Console.WriteLine(output);

The quick _______ dog jumps over ______________ fox
The quick [0] dog jumps over [1] fox

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM