.net Regex.Split

本文关键字:Split Regex net | 更新日期: 2023-09-27 18:31:54

我正在尝试将以下字符串"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:

你的回应很接近。但以下内容仍未正确拆分:

 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'

.net Regex.Split

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

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中删除中的所有空字符串(这比使用直接的循环慢):

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'
|

补充说明:

如果你想拆分任何其他运算符(例如,<<+==-=>>)以及(有很多),或者需要其他任何东西,请询问。

感谢"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*");