简体   繁体   English

Regex.Split 空白

[英]Regex.Split White Space

string pattern = @"(if)|(\()|(\))|(\,)";
string str = "IF(SUM(IRS5555.IRs001)==IRS5555.IRS001,10,20)";
string[] substrings = Regex.Split(str,pattern,RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase) ;
foreach (string match in substrings)
{
    Console.WriteLine("Token is:{0}", match);
}

And out put is输出是

Token is:
Token is:IF
Token is:
Token is:(
Token is:SUM
Token is:(
Token is:IRS5555.IRs001
Token is:)
Token is:==IRS5555.IRS001
Token is:,
Token is:10
Token is:,
Token is:20
Token is:)
Token is:

As you can see Empty string in 1,3 and last token,i am not able to understand why this kind of result,there is not empty string in my given string.正如您在 1,3 和最后一个标记中看到的空字符串,我无法理解为什么会出现这种结果,我给定的字符串中没有空字符串。

i don't want this is result我不希望这是结果

try that:试试看:

        string pattern = @"(if)|(\()|(\))|(\,)";
        string str = "IF(SUM(IRS5555.IRs001)==IRS5555.IRS001,10,20)";
        var substrings = Regex.Split(str, pattern, RegexOptions.IgnoreCase).Where(n => !string.IsNullOrEmpty(n));
        foreach (string match in substrings)
        {
            Console.WriteLine("Token is:{0}", match);
        }

在此处输入图像描述

This happens because "IF" and "(" are separators and since there is nothing to the left of "IF" and nothing between "IF" and "(" you get these two empty entries. Remove "IF" from the pattern.发生这种情况是因为“IF”和“(”是分隔符,并且由于“IF”左侧没有任何内容,“IF”和“(”之间也没有任何内容,所以您得到这两个空条目。从模式中删除“IF”。

string pattern = @"(\()|(\))|(\,)"; 

UPDATE更新

You could search for the tokens instead of splitting the string您可以搜索标记而不是拆分字符串

var matches = Regex.Matches(str, @"\w+|[().,]|==");

This returns exacly the tokens of your text.这会准确地返回您的文本的标记。

string[] array = matches.Cast<Match>().Select(m => m.Value).ToArray();
    [0]: "IF"
    [1]: "("
    [2]: "SUM"
    [3]: "("
    [4]: "IRS5555"
    [5]: "."
    [6]: "IRs001"
    [7]: ")"
    [8]: "=="
    [9]: "IRS5555"
    [10]: "."
    [11]: "IRS001"
    [12]: ","
    [13]: "10"
    [14]: ","
    [15]: "20"
    [16]: ")"

UPDATE更新

Another Regex pattern you can try together with Regex.Split is您可以与Regex.Split一起尝试的另一种正则Regex模式是

@"\b"

It will split the text at word boundries它将在单词边界处拆分文本

    [0]: ""
    [1]: "IF"
    [2]: "("
    [3]: "SUM"
    [4]: "("
    [5]: "IRS5555"
    [6]: "."
    [7]: "IRs001"
    [8]: ")=="
    [9]: "IRS5555"
    [10]: "."
    [11]: "IRS001"
    [12]: ","
    [13]: "10"
    [14]: ","
    [15]: "20"
    [16]: ")"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM