[英].net Regex.Split
我正在尝试将以下字符串"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'
我几乎需要正则表达式才能引用。
谢谢
有关lordcheeto答案的更新1:
你的回答很接近。 但以下仍未正确拆分:
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();
}
}
}
测试:
输出:
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.Linq
和System.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();
}
}
}
测试:
输出:
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'
|
附加评论:
如果你想要拆分任何其他运算符(例如, <<
, +=
, =
, -=
, >>
)(有很多 ),或者还需要其他任何操作符,请问。
感谢“lordcheeto”的答案,我能够解决您的解决方案中的类似问题。 我正在分享我的问题和解决方案,以防万一有类似问题的人。
我必须拆分字符串
"abc < 1 && 124 > 2 || 1243 <= 555";
先入
abc < 1
&&
124 > 2
||
1243 <= 555
我通过使用实现了这一点
string[] condtions = Regex.Split(str, @"\s*('.*?'|&&|\|\|)\s*");
然后我必须将每个条件分开
abc < 1
成
abc
<
1
我通过使用实现了这一点
string[] statements = Regex.Split(condtions[0], @"\s*('.*?'|==|<=|>=|<|>|!=)\s*");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.