示例#1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RegexContex"/> class.
 /// </summary>
 /// <param name="content">
 /// 待匹配字符串
 /// </param>
 /// <param name="operater">
 /// 正则操作
 /// </param>
 public RegexContex(string content, RegexOperator operater)
 {
     this.Content  = content;
     this.Operator = operater;
     this.Matches  = new List <string>();
     this.Groups   = new Dictionary <int, List <string> >();
 }
示例#2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RegexContex"/> class. 
 /// </summary>
 /// <param name="content">
 /// 待匹配字符串
 /// </param>
 /// <param name="operater">
 /// 正则操作
 /// </param>
 public RegexContex(string content, RegexOperator operater)
 {
     this.Content = content;
     this.Operator = operater;
     this.Matches = new List<string>();
     this.Groups = new Dictionary<int, List<string>>();
 }
示例#3
0
        public void AssortTheWordsTest()
        {
            var input = "It +is a +great -day +to -live ";
            var regex = new Regex("^(\\w*)");

            var actual = RegexOperator.AssortTheWords(input, regex, 1);
            var expected = new string[] { "It" }.ToList();

            Assert.Equal(actual, expected);
        }
示例#4
0
        //Build a binary tree of operations in the regex
        private static OperationTree buildOperatorTree(string exp)
        {
            OperationTree operations = new OperationTree();

            if (exp.Length == 0)
            {
                return(null);
            }
            //If the entire exp is within brackets, obsolete
            if (exp[0] == '(' && exp[exp.Length - 1] == ')')
            {
                exp = exp.Substring(1, exp.Length - 2);
                #region DEBUG
#if DEBUG
                Console.INSTANCE.WriteLine("Removed brackets: new exp = " + exp);
#endif
                #endregion
            }
            #region DEBUG
#if DEBUG
            Console.INSTANCE.WriteLine("Parsing " + exp + " for highest precedence operator");
#endif
            #endregion
            //The operator for this iteration
            RegexOperator thisOp = new RegexOperator();

            if (exp.Length == 1)
            {
                thisOp = new RegexOperator(RegexOperator.OpType.TERMINAL, exp[0], 0);
            }


            //loop variables
            int          bracketDepth = 0;
            Stack <char> brackets     = new Stack <char>();

            //Behaviour per character
            for (int i = 0; i < exp.Length; i++)
            {
                if (validCharacters.Contains <char>(exp[i])) //Is an operator, check if it's the precedent operator
                {
                    switch (exp[i])
                    {
                    case '(':     //BRACKET
                        //Remember bracket with index
                        bracketDepth--;
                        brackets.Push(exp[i]);
                        break;

                    case ')':     //BRACKET
                        bracketDepth++;
                        char match = brackets.Pop();
                        if (match != '(')
                        {
                            return(null);    //Something went wrong
                        }
                        break;

                    case '+':     //ONE-OR-MORE the #-OR-# operators actually go in sequence
                        if (bracketDepth == 0 && (thisOp.Type < RegexOperator.OpType.ONE_OR_MORE))
                        {
                            thisOp = new RegexOperator(RegexOperator.OpType.ONE_OR_MORE, exp[i], i);
                        }
                        break;

                    case '*':     //ZERO-OR-MORE
                        if (bracketDepth == 0 && (thisOp.Type < RegexOperator.OpType.ZERO_OR_MORE))
                        {
                            thisOp = new RegexOperator(RegexOperator.OpType.ZERO_OR_MORE, exp[i], i);
                        }
                        break;

                    case '?':     //ZERO-OR-ONE
                        if (bracketDepth == 0 && (thisOp.Type < RegexOperator.OpType.ZERO_OR_ONE))
                        {
                            thisOp = new RegexOperator(RegexOperator.OpType.ZERO_OR_ONE, exp[i], i);
                        }
                        break;

                    case '|':     //OR
                        if (bracketDepth == 0 && (thisOp.Type < RegexOperator.OpType.OR))
                        {
                            thisOp = new RegexOperator(RegexOperator.OpType.OR, exp[i], i);
                        }
                        break;

                    case '.':
                        if (bracketDepth == 0 && thisOp.Type < RegexOperator.OpType.DOT)
                        {
                            thisOp = new RegexOperator(RegexOperator.OpType.DOT, '.', i);
                        }
                        break;

                    default:
                        Console.INSTANCE.WriteLine("ERR: Unimplemented character in regex");
                        return(null);
                    }
                }
                else if (char.IsLetterOrDigit(exp[i]))//Language character
                {
                    if (bracketDepth == 0 && thisOp.Type < RegexOperator.OpType.TERMINAL)
                    {
                        thisOp = new RegexOperator(RegexOperator.OpType.TERMINAL, exp[i], i);
                    }
                }
            }
            operations.Op = thisOp;
            string leftRegex  = null;
            string rightRegex = null;
            if (thisOp.Type != RegexOperator.OpType.TERMINAL)
            {
                leftRegex         = exp.Substring(0, thisOp.Index);
                operations.OpLeft = buildOperatorTree(leftRegex);
                if (thisOp.Index != exp.Length)
                {
                    rightRegex         = exp.Substring(thisOp.Index + 1, exp.Length - (thisOp.Index + 1));
                    operations.OpRight = buildOperatorTree(rightRegex);
                }
            }
            #region DEBUG
#if DEBUG
            Console.INSTANCE.WriteLine("Highest presedence op : " + thisOp.Character + " in " + exp);
            if (leftRegex != null)
            {
                Console.INSTANCE.WriteLine("Left : " + leftRegex);
            }
            else
            {
                Console.INSTANCE.WriteLine("No left hand side");
            }
            if (rightRegex != null)
            {
                Console.INSTANCE.WriteLine("Right : " + rightRegex);
            }
            else
            {
                Console.INSTANCE.WriteLine("No right hand side");
            }
#endif
            #endregion
            return(operations);
        }