gpt4 book ai didi

c# - .net 正则表达式拆分

转载 作者:太空狗 更新时间:2023-10-30 01:24:07 24 4
gpt4 key购买 nike

我正在尝试拆分以下字符串 "Name=='mynme' && CurrentTime<'2012-04-20 19:45:45'"进入这个:

Name
==
'myname'
&&
CurrentTime
<
'2012-04-20 19:45:45'

我有以下正则表达式:

([+\\-*/%()]{1}|[=<>!]{1,2}|[&|]{2})

问题是在使用上面的正则表达式时我得到以下结果:

Name
==
'myname'
&&
CurrentTime
<
'2012
-
04
-
20
19:45:45'

我实际上需要正则表达式来识别引号。

谢谢

更新 1 关于 lordcheeto 的回答:

您的回复很接近。但以下仍然没有正确拆分:

 string input2 = "((1==2) && 2-1==1) || 3+1==4 && Name=='Stefan+123'";

我需要做的是将字符串拆分为运算符和操作数。像这样:

 LeftOperand Operator RightOperand

现在,如果任何运算符介于 '' 之间它应该被忽略,整个字符串在 '' 之间应被视为操作数。

上面的字符串应该生成以下输出:

(

(
1
==
2
)

&&
2
-
1
==
1
)

||
3
+
1
==
4
&&
Name
==
'Stefan+123'

最佳答案

好的,假设您希望它简单地拆分逻辑和关系运算符,您可以使用这种模式:

string lordcheeto = @"\s*(==|&&|<=|>=|<|>)\s*";    

这还将从返回的字符串中删除所有空格。

代码:

using System;
using System.Text.RegularExpressions;

namespace RegEx
{
class Program
{
static void Main(string[] args)
{
string original = "([+\\-*/%()]{1}|[=<>!]{1,2}|[&|]{2})";
string lordcheeto = @"\s*(==|&&|<=|>=|<|>)\s*";

string input = "Name=='mynme' && CurrentTime<45 - 4";
string input1 = "Name=='mynme' && CurrentTime<'2012-04-20 19:45:45'";
string ridiculous = "Name == BLAH && !@#>=$%^&*()< ASDF && this > that";

executePattern("original", input, original);
executePattern("lordcheeto's", input, lordcheeto);
executePattern("original", input1, original);
executePattern("lordcheeto's", input1, lordcheeto);
executePattern("original", ridiculous, original);
executePattern("lordcheeto's", ridiculous, lordcheeto);
}

static void executePattern(string version, string input, string pattern)
{
// Avoiding repitition for this example.
Console.WriteLine("Using {0} pattern:", version);

// Needs to be trimmed.
var result = Regex.Split(input.Trim(), pattern);

// Pipes included to highlight whitespace trimming.
foreach (var m in result)
Console.WriteLine("|{0}|", m);

// Extra space.
Console.WriteLine();
Console.WriteLine();
}
}
}

测试:

http://goo.gl/XAm6J

输出:

Using original pattern:
|Name|
|==|
|'mynme' |
|&&|
| CurrentTime|
|<|
|45 |
|-|
| 4|


Using lordcheeto's pattern:
|Name|
|==|
|'mynme'|
|&&|
|CurrentTime|
|<|
|45 - 4|


Using original pattern:
|Name|
|==|
|'mynme' |
|&&|
| CurrentTime|
|<|
|'2012|
|-|
|04|
|-|
|20 19:45:45'|


Using lordcheeto's pattern:
|Name|
|==|
|'mynme'|
|&&|
|CurrentTime|
|<|
|'2012-04-20 19:45:45'|


Using original pattern:
|Name |
|==|
| BLAH |
|&&|
| |
|!|
|@#|
|>=|
|$|
|%|
|^&|
|*|
||
|(|
||
|)|
||
|<|
| ASDF |
|&&|
| this |
|>|
| that|


Using lordcheeto's pattern:
|Name|
|==|
|BLAH|
|&&|
|!@#|
|>=|
|$%^&*()|
|<|
|ASDF|
|&&|
|this|
|>|
|that|

编辑

好的,有了额外的限制,你应该可以使用这个:

string lordcheeto = @"\s*('.*?'|&&|==|<=|>=|<|>|\(|\)|\+|-|\|\|)\s*";

这仍然会从返回的字符串中删除所有空格。但是,如果匹配项彼此相邻(例如 Name=='Stefan+123' ),它将返回空字符串。这次我无法解决这个问题,但这并不重要。

如果你导入System.LinqSystem.Collections.Generic并使结果成为 List<string> , 您可以从 List 中删除所有空字符串在像这样的额外一行中(这比直接使用 for 循环要慢):

var results = Regex.Split(input.Trim(), pattern).ToList();
results.RemoveAll(x => x == "");

代码:

using System;
using System.Text.RegularExpressions;

namespace RegEx
{
class Program
{
static void Main(string[] args)
{
string lordcheeto = @"\s*('.*?'|&&|==|<=|>=|<|>|\(|\)|\+|-|\|\|)\s*";

string input = "Name=='mynme' && CurrentTime<45 - 4";
string input1 = "Name=='mynme' && CurrentTime<'2012-04-20 19:45:45'";
string input2 = "((1==2) && 2-1==1) || 3+1==4 && Name=='Stefan+123'";

executePattern("lordcheeto's", input, lordcheeto);
executePattern("lordcheeto's", input1, lordcheeto);
executePattern("lordcheeto's", input2, lordcheeto);

Console.ReadLine();
}

static void executePattern(string version, string input, string pattern)
{
// Avoiding repitition for this example.
Console.WriteLine("Using {0} pattern:", version);

// Needs to be trimmed.
var result = Regex.Split(input.Trim(), pattern);

// Pipe included to highlight empty strings.
foreach (var m in result)
Console.WriteLine("|{0}", m);

// Extra space.
Console.WriteLine();
Console.WriteLine();
}
}
}

测试:

http://goo.gl/lkaoM

输出:

Using lordcheeto's pattern:
|Name
|==
|
|'mynme'
|
|&&
|CurrentTime
|<
|45
|-
|4


Using lordcheeto's pattern:
|Name
|==
|
|'mynme'
|
|&&
|CurrentTime
|<
|
|'2012-04-20 19:45:45'
|


Using lordcheeto's pattern:
|
|(
|
|(
|1
|==
|2
|)
|
|&&
|2
|-
|1
|==
|1
|)
|
|||
|3
|+
|1
|==
|4
|&&
|Name
|==
|
|'Stefan+123'
|

附加评论:

如果您想拆分任何其他运算符(例如 <<+==-=>>)以及(有 a lot),或者需要其他任何东西,只需问。

关于c# - .net 正则表达式拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10255265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com