检查圆括号前后是否有空白
本文关键字:空白 是否 圆括号 检查 | 更新日期: 2023-09-27 17:52:45
我的字符串可以有多个开括号和闭括号,并希望在我们使用这样的字符串用于电子邮件分发之前检查它的语法正确。字符串来自供应商。
如果之前和/或之后没有空格,我想添加一个空白。
当前,代码将检查所有(
,并检查index-1
中的字符是否为空白,如果不是,则添加它,然后对)
进行类似的操作。
这是我们得到的供应商代码之一,并被告知它可以工作,因为字符串最多有40个字符。
我可以使用正则表达式检查和添加空间吗?我看了看SO,到目前为止还没有发现任何东西,发现文章使用Regex从某些字符中提取文本。
下面是插入空格的Non Regex和将2个空格减少为1个空格的Regex的组合
string text = " ( ( (abc ) def)ghi)";
text = Regex.Replace(text.Replace("(", " ( ").Replace(")", " ) "), @"[ ]{2,}", @" ");
Console.WriteLine(text);
这是一个小清洁剂,有一个String.Replace()
&一个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()
,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
这似乎行得通。如果有两个相邻的,你会得到两个空格。
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.Run();
}
public void Run()
{
this.Attempt("");
this.Attempt("(");
this.Attempt(")");
this.Attempt("()");
this.Attempt("( ");
this.Attempt(" )");
this.Attempt("( )");
this.Attempt(" ( ");
this.Attempt(" ( ( ");
}
private void Attempt(string original)
{
System.Console.WriteLine("'" + original + "' => '" + this.Replace(original) + "'" );
}
private string Replace(string original)
{
string regex = @"('s*(')|'()'s*)";
Regex replacer = new Regex(regex);
string replaced = replacer.Replace(original, " $2 ");
return replaced;
}
结果:
'' => ''
'(' => ' ( '
')' => ' ) '
'()' => ' ( ) '
'( ' => ' ( '
' )' => ' ) '
'( )' => ' ( ) '
' ( ' => ' ( '
' ( ( ' => ' ( ( '
你可以这样做:
string text = " ( ((abc ) def)ghi)";
text = text.Replace("(","( ").Replace(")"," )");//Add aditional space no matter if this one already have it.
while(text.Contains(" ")){//===>To while text contains double spaces
text = text.Replace(" "," ");//===>Replace spaces for a single space;
}