Ejemplo n.º 1
0
        /// <summary>
        /// Reg expression 3 testData
        /// </summary>
        public TestRegExp()
        {
            a = new RegExpression("a");
            b = new RegExpression("b");

            // expr1: "baa"
            expr1 = new RegExpression("baa");
            // expr2: "bb"
            expr2 = new RegExpression("bb");
            // expr3: "baa | baa"
            expr3 = expr1.Or(expr2);

            // all: "(a|b)*"
            all = (a.Or(b)).Star();

            // expr4: "(baa | baa)+"
            expr4 = expr3.Plus();

            // expr5: "(baa | baa)+ (a|b)*"
            expr5 = expr4.Dot(all);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Converts a string to a regExpressionObject.
        /// </summary>
        /// <param name="regexString">The regex string.</param>
        /// <returns></returns>
        public RegExpression StringToRegExpression(RegExpression regex, string regexString)
        {
            // Remove spaces
            regexString = regexString.Replace(" ", String.Empty);

            // Seperate all terminals
            char[]        seperators    = { '+', '*', '|', '(', ')' };
            List <string> terminalParts = regexString.Split(seperators).ToList();

            terminalParts.RemoveAll(x => x == String.Empty);
            int terminalIndex = 0;
            int maxTerminals  = terminalParts.Count();

            int i = 0;

            while (i < regexString.Length)
            {
                char currentChar = regexString[i];

                // For everything between ( )
                if (currentChar == '(')
                {
                    int closingBracketPosition = -1;
                    int bracketCount           = 0;
                    for (int j = i + 1; i < regexString.Length; j++)
                    {
                        if (regexString[j] == '(')
                        {
                            bracketCount++;
                        }

                        // We found the matching closing bracket
                        if (regexString[j] == ')' && bracketCount == 0)
                        {
                            closingBracketPosition = j;
                            break;
                        }
                        if (regexString[j] == ')' && bracketCount != 0)
                        {
                            bracketCount--;
                        }
                    }

                    // Get the regex for the part between  ()
                    string        between       = regexString.Substring(i + 1, closingBracketPosition - 1 - i);
                    RegExpression regExpression = StringToRegExpression(new RegExpression(), between);

                    // Look for the part after closing bracket
                    if (closingBracketPosition + 1 < regexString.Length)
                    {
                        i           = closingBracketPosition + 1;
                        currentChar = regexString[i];
                        if (currentChar == '+')
                        {
                            regExpression = regExpression.Plus();
                        }
                        else if (currentChar == '*')
                        {
                            regExpression = regExpression.Star();
                        }
                    }

                    if (regex.terminals == "" && regex.o == Operator.ONE)
                    {
                        regex = regExpression;
                    }
                    else
                    {
                        regex = regex.Dot(regExpression);
                    }
                }

                // For all the operators not related to ()
                else if (currentChar == '+')
                {
                    regex = regex.Plus();
                }
                else if (currentChar == '*')
                {
                    regex = regex.Star();
                }
                else if (currentChar == '|')
                {
                    regex = regex.Or(new RegExpression(terminalParts[terminalIndex].ToString()));
                    terminalIndex++;
                    i++;
                }
                else
                {
                    if (regex.terminals == "" && regex.o == Operator.ONE)
                    {
                        regex.terminals = terminalParts[terminalIndex];

                        // Skip rest of the terminal part in the loop
                        int num = regex.terminals.Count();
                        i += terminalParts[terminalIndex].Length - 1;
                        terminalIndex++;
                    }
                    else
                    {
                        //regex = regex.Dot(new RegExpression(currentChar.ToString()));
                    }
                }
                i++;
            }


            //for (int i = 0; i < regexString.Length; i++)
            //{
            //	char currentChar = regexString[i];

            //	// For everything between ( )
            //	if (currentChar == '(')
            //	{
            //		int closingBracketPosition = -1;
            //		int bracketCount = 0;
            //		for (int j = i + 1; i < regexString.Length; j++)
            //		{
            //			if (regexString[j] == '(') bracketCount++;

            //			// We found the matching closing bracket
            //			if (regexString[j] == ')' && bracketCount == 0)
            //			{
            //				closingBracketPosition = j;
            //				break;
            //			}
            //			if (regexString[j] == ')' && bracketCount != 0)
            //			{
            //				bracketCount--;
            //			}
            //		}

            //		// Get the regex for the part between  ()
            //		string between = regexString.Substring(i + 1, closingBracketPosition - 1 - i);
            //		RegExpression regExpression = StringToRegExpression(new RegExpression(), between);

            //		// Look for the part after closing bracket
            //		if (closingBracketPosition + 1 < regexString.Length)
            //		{
            //			i = closingBracketPosition + 1;
            //			currentChar = regexString[i];
            //			if (currentChar == '+')
            //			{
            //				regExpression = regExpression.Plus();
            //			}
            //			else if (currentChar == '*')
            //			{
            //				regExpression = regExpression.Star();
            //			}
            //		}

            //		if (regex.terminals == "" && regex.o == Operator.ONE)
            //		{
            //			regex = regExpression;
            //		}
            //		else
            //		{
            //			regex = regex.Dot(regExpression);
            //		}
            //	}

            //	// For all the operators not related to ()
            //	else if (currentChar == '+')
            //	{
            //		regex = regex.Plus();
            //	}
            //	else if (currentChar == '*')
            //	{
            //		regex = regex.Star();
            //	}
            //	else if (currentChar == '|')
            //	{
            //		regex = regex.Or(new RegExpression(terminalParts[terminalIndex].ToString()));
            //		terminalIndex++;
            //		i++;
            //	}
            //	else
            //	{
            //		if (regex.terminals == "" && regex.o == Operator.ONE)
            //		{
            //			regex.terminals = terminalParts[terminalIndex];

            //			// Skip rest of the terminal part in the loop
            //			int num = regex.terminals.Count();
            //			i += terminalParts[terminalIndex].Length - 1;
            //			terminalIndex++;
            //		}
            //		else
            //		{
            //			//regex = regex.Dot(new RegExpression(currentChar.ToString()));
            //		}
            //	}

            //}
            return(regex);
        }