使用 C# 删除分隔符

发布于 2024-09-12 06:45:04 字数 155 浏览 3 评论 0原文

我有一个字符串如下:

{a{b,c}d}

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

{a d} 

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

有人可以帮我吗?

I Have a string as follows:

{a{b,c}d}

if i give 1, the string must be displayed as:

{a d} 

the content within inner braces should be removed along with the braces.

Can anyone help me with it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走过海棠暮 2024-09-19 06:45:04

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

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");

To extract the inner grouping of {} use the following regular expression:

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

Actually, if you want to remove the comma....

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

To extract the outer without the inner grouping:

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

if in your example a, b, c, d are not literally single characters, that is groups of letters or even spaces, etc replace \w with \w+ or even .*

based on your comment on nesting....

string extract = Regex.Replace(source, "(\{\w)\{.*\}(\w\})\w*", "$1 $2");
翻了热茶 2024-09-19 06:45:04

按照上面的正则表达式方式...它真的更漂亮!


你可以手工完成......几年前我在一个例子中写了一些括号......必须找一秒钟......:

     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

go the regex-way above... it's really more beautiful!


you could do it by hand... i've written something for paranthesis in an example a few years ago... have to look for it a sec...:

     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);
     }

writes:

1+2*
3/(4+5)
*2
情绪操控生活 2024-09-19 06:45:04

命名空间分隔符
{
班级计划
{
静态无效主(字符串[]参数)
{
字符串 src = "a{b{c{d,e}f}g}h";
int 发生次数 = 0;
foreach(src 中的 char ch)
{
if(ch == '{')
{
发生次数++;
}
}
Console.WriteLine("请输入要删除的方块编号:");
int 给定值 = 0;
检查有效(出给定值);

        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);
        }
    }
}

}

namespace Delimiter
{
class Program
{
static void Main(string[] args)
{
string src = "a{b{c{d,e}f}g}h";
int occurenceCount = 0;
foreach (char ch in src)
{
if(ch == '{')
{
occurenceCount++;
}
}
Console.WriteLine("Enter a no. to remove block: ");
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);
        }
    }
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文