Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .net正则表达式.Split_C#_.net_Regex - Fatal编程技术网

C# .net正则表达式.Split

C# .net正则表达式.Split,c#,.net,regex,C#,.net,Regex,我正在尝试拆分以下字符串“Name=='mynme'&&CurrentTime好的,假设您希望它只在逻辑运算符和关系运算符上拆分,您可以使用此模式: string lordcheeto = @"\s*(==|&&|<=|>=|<|>)\s*"; 代码: using System; using System.Text.RegularExpressions; namespace RegEx { class Program {

我正在尝试拆分以下字符串
“Name=='mynme'&&CurrentTime好的,假设您希望它只在逻辑运算符和关系运算符上拆分,您可以使用此模式:

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|
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'
|
使用系统;
使用System.Text.RegularExpressions;
名称空间正则表达式
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串lordcheeto=@“\s*('.*?'&&&&&&&&=”===(\\)\+-\\\\\\\\\\\\\\)\s*;
string input=“Name=='mynme'&&CurrentTime感谢“lordcheeto”的回答,我能够用您的解决方案解决类似的问题。我将分享我的问题和解决方案,以防帮助任何有类似问题的人

我必须像这样把绳子分开

"abc < 1 && 124 > 2 || 1243 <= 555";
然后我要把每个条件分解成

abc < 1 
abc<1
进入

abc
<
1.
我通过使用

string[] condtions = Regex.Split(str, @"\s*('.*?'|&&|\|\|)\s*");
string[] statements = Regex.Split(condtions[0], @"\s*('.*?'|==|<=|>=|<|>|!=)\s*");
string[]语句=Regex.Split(条件[0],@“\s*('.*?'|===|=| |!=)\s*”;

谢谢你的回答。请查看我在原始帖子上的更新。刚刚编辑过,现在应该可以用了。
string lordcheeto = @"\s*('.*?'|&&|==|<=|>=|<|>|\(|\)|\+|-|\|\|)\s*";
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'
|
"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*");