条件逻辑串的解析和正则表达式

时间:2013-07-09 08:05:18

标签: c# regex string parsing string-parsing

我必须标记条件字符串表达式:

  

Aritmetic运算符是= +, - ,*,/,%

     

布尔运算符=&&,||

     

条件运算符= ==,> =,>,<,< =,<,!=

示例表达式是=(x + 3> 5 * y)&&(z> = 3 || k!= x)

我想要的是标记化这个字符串=运算符+操作数。

因为“>”和“> =”和“=”和“!=”[包含相同的字符串]我在标记化方面遇到问题。

PS1 :我不想进行复杂的词法分析。只是简单地解析 如果可能的话,用reqular表达式。

PS2:或者换句话说,我寻找给出的正则表达式 样本表达式没有whitespace =

(x+3>5*y)&&(z>=3 || k!=x) 

并将生成每个标记用白色空格分隔,如:

( x + 3 > 5 * y ) && ( z >= 3 || k != x )

2 个答案:

答案 0 :(得分:4)

不是正则表达式,而是可能正常工作的基本标记器(请注意,您不需要执行string.Join - 您可以使用IEnumerable<string>通过foreach):< / p>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
static class Program
{
    static void Main()
    {
        // and will produce each token is separated with a white space like : ( x + 3 > 5 * y ) && ( z >= 3 || k != x )
        string recombined = string.Join(" ", Tokenize("(x+3>5*y)&&(z>=3 || k!=x)"));
        // output: ( x + 3 > 5 * y ) && ( z >= 3 || k != x )
    }
    public static IEnumerable<string> Tokenize(string input)
    {
        var buffer = new StringBuilder();
        foreach (char c in input)
        {
            if (char.IsWhiteSpace(c))
            {
                if (buffer.Length > 0)
                {
                    yield return Flush(buffer);
                }
                continue; // just skip whitespace
            }

            if (IsOperatorChar(c))
            {
                if (buffer.Length > 0)
                {
                    // we have back-buffer; could be a>b, but could be >=
                    // need to check if there is a combined operator candidate
                    if (!CanCombine(buffer, c))
                    {
                        yield return Flush(buffer);
                    }
                }
                buffer.Append(c);
                continue;
            }

            // so here, the new character is *not* an operator; if we have
            // a back-buffer that *is* operators, yield that
            if (buffer.Length > 0 && IsOperatorChar(buffer[0]))
            {
                yield return Flush(buffer);
            }

            // append
            buffer.Append(c);
        }
        // out of chars... anything left?
        if (buffer.Length != 0)
            yield return Flush(buffer);
    }
    static string Flush(StringBuilder buffer)
    {
        string s = buffer.ToString();
        buffer.Clear();
        return s;
    }
    static readonly string[] operators = { "+", "-", "*", "/", "%", "=", "&&", "||", "==", ">=", ">", "<", "<=", "!=", "(",")" };
    static readonly char[] opChars = operators.SelectMany(x => x.ToCharArray()).Distinct().ToArray();

    static bool IsOperatorChar(char newChar)
    {
        return Array.IndexOf(opChars, newChar) >= 0;
    }
    static bool CanCombine(StringBuilder buffer, char c)
    {
        foreach (var op in operators)
        {
            if (op.Length <= buffer.Length) continue;
            // check starts with same plus this one
            bool startsWith = true;
            for (int i = 0; i < buffer.Length; i++)
            {
                if (op[i] != buffer[i])
                {
                    startsWith = false;
                    break;
                }
            }
            if (startsWith && op[buffer.Length] == c) return true;
        }
        return false;
    }

}

答案 1 :(得分:1)

如果你可以预定义你将要使用的所有操作符,这样的东西可能适合你。

请务必将双字符运算符放在正则表达式的前面,以便您尝试匹配'&lt;'在匹配'&lt; ='之前。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "!=|<=|>=|\\|\\||\\&\\&|\\d+|[a-z()+\\-*/<>]";
      string sentence = "(x+35>5*y)&&(z>=3 || k!=x)";

      foreach (Match match in Regex.Matches(sentence, pattern))
         Console.WriteLine("Found '{0}' at position {1}", 
                           match.Value, match.Index);
   }
}

输出:

Found '(' at position 0
Found 'x' at position 1
Found '+' at position 2
Found '35' at position 3
Found '>' at position 5
Found '5' at position 6
Found '*' at position 7
Found 'y' at position 8
Found ')' at position 9
Found '&&' at position 10
Found '(' at position 12
Found 'z' at position 13
Found '>=' at position 14
Found '3' at position 16
Found '||' at position 18
Found 'k' at position 21
Found '!=' at position 22
Found 'x' at position 24
Found ')' at position 25
相关问题