使用C#删除分隔符

时间:2010-07-30 08:41:01

标签: c# c#-3.0 delimiter

我有一个字符串如下:

{a{b,c}d}

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

{a d} 

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

任何人都可以帮我吗?

3 个答案:

答案 0 :(得分:5)

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

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 :(得分:1)

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


你可以手工完成......几年前我在一个例子中写了一些关于paranthesis的内容......必须在一段时间内寻找它......:

     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

答案 2 :(得分:0)

命名空间分隔符 {     课程     {         static void Main(string [] args)         {             string src =“a {b {c {d,e} f} g} h”;             int occurenceCount = 0;             foreach(src中的char ch)             {                 if(ch =='{')                 {                     occurenceCount ++;                 }             }             Console.WriteLine(“输入一个删除块:”);             int givenValue = 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);
        }
    }
}

}