在打开和关闭括号之前检查是否存在空格

时间:2015-06-01 17:35:32

标签: c#

我的字符串可能有多个打开和关闭的括号,并且在我们使用这样的字符串进行电子邮件分发之前,想要检查它是否在语法上是正确的。字符串来自供应商。

如果在我想要添加空格之前和/或之后没有空格。

目前代码通过并检查或全部(并检查index-1处的字符是否为空格,如果没有添加,则)的字符类似。< / p>

这是我们给出的供应商代码之一,并被告知它的工作原理是字符串最多为40个字符。

我可以使用正则表达式检查和添加空间吗?我看了SO,到目前为止还没找到任何东西,找到了使用Regex从某些字符中提取文本的文章。

3 个答案:

答案 0 :(得分:1)

这是非正则表达式插入空格的组合,而正则表达式则将2个空格缩小为1个空格

string text = " ( ( (abc ) def)ghi)";
text = Regex.Replace(text.Replace("(", " ( ").Replace(")", " ) "), @"[ ]{2,}", @" ");
Console.WriteLine(text);

这里有一个String.Replace()&amp;一个Regex.Replace()

string text = " ( ((abc ) def)ghi)";
text = Regex.Replace(text.Replace(" ", String.Empty), "\\w+|[()]", "$0 ");

Console.WriteLine(text);

结果:

 (  (  ( abc ) def ) ghi )

更新

Regex和非Regex之间的表现一整天都在我脑海中,所以我决定测试我已经针对纯非Regex方法提供的两个样本

您将在下面的代码中看到InsertSpaces1()是我的第一个样本,而InsertSpaces2()是我的第二个样本。 InsertSpaces3()&amp; InsertSpaces4()和在括号前后插入空格的纯非Regex方法。 InsertSpaces3()将数据保存在StringBuilder中,而InsertSpaces4()将数据保存在string中。事实证明,InsertSpaces4()是实现结果的最快方式,即使它可能不是最有效的内存,因为每个Insert()调用都会生成新的字符串。 InsertSpaces3()排在第二位,但在内存使用方面效率可能更高。

using System;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        InsertSpaces1();
        InsertSpaces2();
        InsertSpaces3();
        InsertSpaces4();
    }

    private static void InsertSpaces1()
    {
        Stopwatch w = new Stopwatch();
        w.Start();

        string text = " ( ((abc ) def)ghi)";
        text = Regex.Replace(text.Replace("(", " ( ").Replace(")", " ) "), @"[ ]{2,}", @" ");
        Console.WriteLine(text);

        w.Stop();
        Console.WriteLine(w.Elapsed);
    }

    private static void InsertSpaces2()
    {
        Stopwatch w = new Stopwatch();
        w.Start();

        string text = " ( ((abc ) def )ghi)";
        text = Regex.Replace(text.Replace(" ", String.Empty), "\\w+|[()]", "$0 ");
        Console.WriteLine(text);

        w.Stop();
        Console.WriteLine(w.Elapsed);
    }

    private static void InsertSpaces3()
    {
        Stopwatch w = new Stopwatch();
        w.Start();

        StringBuilder text = new StringBuilder("( ((abc ) def )ghi)");
        for (int i = 0; i < text.Length; i++) 
        {
            if (
                // Insert a space to the left even at the beginning of the string
                (text[i] == '(' && ((i - 1 >= 0 && text[i - 1] != ' ') || i == 0)) ||
                (text[i] == ')' && ((i - 1 >= 0 && text[i - 1] != ' ') || i == 0))
               )
            {
                text.Insert(i, ' ');
            } 
            else if (
                     // Insert a space to the right
                     (text[i] == '(' && (i + 1 < text.Length && text[i + 1] != ' ')) ||
                     (text[i] == ')' && (i + 1 < text.Length && text[i + 1] != ' '))
                    )
            {
                text.Insert(i + 1, ' ');
            }
            else if (
                     // Insert a space to the right even at the end
                     (text[i] == '(' && (i + 1 == text.Length)) ||
                     (text[i] == ')' && (i + 1 == text.Length))
                    )
            {
                text.Append(" ");
            }
        }
        Console.WriteLine(text);

        w.Stop();
        Console.WriteLine(w.Elapsed);
    }

    private static void InsertSpaces4()
    {
        Stopwatch w = new Stopwatch();
        w.Start();

        string text = "( ((abc ) def )ghi)";
        for (int i = 0; i < text.Length; i++) 
        {
            if (
                // Insert a space to the left even at the beginning of the string
                (text[i] == '(' && ((i - 1 >= 0 && text[i - 1] != ' ') || i == 0)) ||
                (text[i] == ')' && ((i - 1 >= 0 && text[i - 1] != ' ') || i == 0))
               )
            {
                text = text.Insert(i, " ");
            } 
            else if (
                     // Insert a space to the right
                     (text[i] == '(' && (i + 1 < text.Length && text[i + 1] != ' ')) ||
                     (text[i] == ')' && (i + 1 < text.Length && text[i + 1] != ' '))
                    )
            {
                text = text.Insert(i + 1, " ");
            }
            else if (
                     // Insert a space to the right even at the end
                     (text[i] == '(' && (i + 1 == text.Length)) ||
                     (text[i] == ')' && (i + 1 == text.Length))
                    )
            {
                text += " ";
            }
        }
        Console.WriteLine(text);

        w.Stop();
        Console.WriteLine(w.Elapsed);
    }
}

结果:

 ( ( ( abc ) def ) ghi ) 
00:00:00.0000383
( ( ( abc ) def ) ghi ) 
00:00:00.0000333
 ( ( ( abc ) def ) ghi ) 
00:00:00.0000114
 ( ( ( abc ) def ) ghi ) 
00:00:00.0000080

请在此处查看工作示例... https://dotnetfiddle.net/21IRX9

答案 1 :(得分:0)

这似乎有效。如果彼此相邻,那么你会得到两个空格。

'' => ''
'(' => ' ( '
')' => ' ) '
'()' => ' (  ) '
'( ' => ' ( '
' )' => ' ) '
'( )' => ' (  ) '
' ( ' => ' ( '
' ( ( ' => ' (  ( '

结果:

{{1}}

答案 2 :(得分:0)

你可以这样做:

Thread t = new Thread(new WeatherYahoo()).start();
t.join();
Weather weather = new Weather();