【问题标题】:Remove delimiter using C#使用 C# 删除分隔符
【发布时间】:2010-07-30 08:41:00
【问题描述】:

我有一个字符串如下:

{a{b,c}d}

如果我给1,字符串必须显示为:

{a d} 

内大括号内的内容应与大括号一起删除。

谁能帮帮我?

【问题讨论】:

  • “如果我给 1”。给什么?你到底想达到什么目的?
  • 字符串总是这样构建的吗? {{,}}
  • 如何处理多级嵌套?
  • 如果要删除的大括号对是 1,那么我必须删除内大括号及其内容
  • 并不总是......它可以是,{a{b{c,d,e}f}g}h

标签: c# c#-3.0 delimiter


【解决方案1】:

要提取 {} 的内部分组,请使用以下正则表达式:

string extract = Regex.Replace(source, "\{\w(\{\w,\w\})\w\}", "$1");

其实如果要去掉逗号的话....

string extract = Regex.Replace(source, "\{\w\{(\w),(\w)\}\w\}", "{$1 $2}");

在没有内部分组的情况下提取外部:

string extract = Regex.Replace(source, "(\{\w)\{\w,\w\}(\w\})", "$1 $2");

如果在您的示例中 a、b、c、d 不是字面上的单个字符,即字母组甚至空格等,请将 \w 替换为 \w+甚至 .*

根据您对嵌套的评论......

string extract = Regex.Replace(source, "(\{\w)\{.*\}(\w\})\w*", "$1 $2");

【讨论】:

  • 该死的……正则表达式在这里真的更有价值……为什么我不这么认为……绝对是我的 +1!
  • 刚刚将 \s 替换为 \w... 抱歉 \s 是空格,我还没有喝咖啡... (\w) 是数字、下划线或字符。哦,谢谢!
  • 抱歉,{ } 需要通过 \ 转义。我将编辑帖子。
【解决方案2】:

去上面的正则表达式...它真的更漂亮!


您可以手动完成...几年前我在一个示例中为括号写了一些东西...必须寻找一下...:

     string def = "1+2*(3/(4+5))*2";
     int pcnt = 0, start = -1, end = -1;
     bool subEx = false;
     if(def.Contains("(") || def.Contains(")"))
        for(int i = 0; i < def.Length; i++) {
           if(def[i] == '(') {
              pcnt++;
              if(!subEx)
                 start = i;
           } else if(def[i] == ')')
              pcnt--;
           if(pcnt < 0)
              throw new Exception("negative paranthesis count...");
           if(pcnt != 0)
              subEx = true;
           if(subEx && pcnt == 0 && end == -1)
              end = i;
        }
     if(pcnt != 0) {
        throw new Exception("paranthesis doesn't match...");
     }
     if(subEx) {
        string firstPart = def.Substring(0, start);
        string innerPart = def.Substring(start + 1, end - (start + 1));
        string secondPart = def.Substring(end + 1);
        Console.WriteLine(firstPart);
        Console.WriteLine(innerPart);
        Console.WriteLine(secondPart);
     }

写道:

1+2*
3/(4+5)
*2

【讨论】:

  • 谢谢你。但我也必须删除大括号内的内容。
【解决方案3】:

命名空间分隔符 { 课堂节目 { 静态无效主要(字符串 [] 参数) { 字符串 src = "a{b{c{d,e}f}g}h"; int发生计数= 0; foreach(src 中的字符 ch) { 如果(通道 == '{') { 出现次数++; } } Console.WriteLine("输入要删除块的编号:"); 整数给定值 = 0; CheckValid(out givenValue);

        int removeCount = occurenceCount + 1 - givenValue;
        occurenceCount = 0;
        int startPos = 0;
        for (int i = 0; i < src.Length; i++)
        {
            if (src[i] == '{')
            {
                occurenceCount++;
            }   
            if(occurenceCount == removeCount)
            {
                startPos = i;
                break;
                //i value { of to be removed block
            }
        }
        int endPos = src.IndexOf('}', startPos);
        src = src.Remove(startPos,endPos);

        //index of }
        Console.WriteLine("after reved vale:" + src);
        Console.ReadKey();
    }

    public static void CheckValid(out int givenValue)
    {
        if (!int.TryParse(Console.ReadLine(), out givenValue))
        {
            Console.WriteLine("Enter a valid no. to remove block: ");
            CheckValid(out givenValue);
        }
    }
}

}

【讨论】:

  • 但这里的问题在于以下行: src = src.Remove(startPos,endPos);我没有删除确切的东西。
猜你喜欢
  • 1970-01-01
  • 2015-07-26
  • 1970-01-01
  • 1970-01-01
  • 2010-12-20
  • 2018-06-19
  • 2017-08-19
  • 1970-01-01
  • 2016-03-13
相关资源
最近更新 更多