예제 #1
0
        /// <summary>
        /// Returns a random string, conforming to the provided regular expression pattern.
        /// </summary>
        /// <param name="regex">The regular expression, which the generated string should conform to.</param>
        /// <param name="seed">The random number generator seed.</param>
        /// <returns>A string, conforming to the provided regular expression pattern.</returns>
        ///
        /// <example>
        /// The following example demonstrates how to generate a random string from a Regex pattern:
        /// <code lang="C#" >
        /// // Using a custom regex pattern...
        /// Regex emailAddress1 = new Regex(@"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$");
        /// string s1 = StringFactory.GenerateRandomString(emailAddress1, 1234);
        ///
        /// // Using a standard regex pattern...
        /// Regex emailAddress2 = CommonRegexPatterns.EmailAddress;
        /// string s2 = StringFactory.GenerateRandomString(emailAddress2, 1234);
        /// </code>
        /// </example>
        public static string GenerateRandomString(Regex regex, int seed)
        {
            Random random = new Random(seed);

            //reset the static variables
            RegexCompiler.IsInvalidSection = false;
            RegexCompiler.InvalidNode      = null;
            RegexCompiler.InvalidableNodes.Clear();

            //construct the RegEx tree
            RegexCompiler compiler = new RegexCompiler();
            RegexNode     node     = compiler.Compile(regex.ToString());

            //search for a signal to invalidate a node
            if (regex.ToString().IndexOf("\\i") != -1)
            {
                //something should have been invalidated
                //select a node to invalidate
                if (RegexCompiler.InvalidableNodes.Count == 0)
                {
                    throw new ArgumentException("Asked to generate invalid: Impossible to invalidate");
                }
                RegexCompiler.InvalidNode = RegexCompiler.InvalidableNodes[random.Next(RegexCompiler.InvalidableNodes.Count)];

                //Mark REOrNodes and RERepeatNodes to ensure that the invalid node will be part of the string
                RegexCompiler.InvalidNode.ReservePath(null);
            }

            //generate and return the string
            string result = node.Generate(random);

            if (RegexCompiler.InvalidNode != null)
            {
                //confirm that the generated string is invalid (e.g. [a-z]|[^a-z] will always fail)
                Regex compare = new Regex("^" + regex.Replace("\\i", "") + "$");
                if (compare.IsMatch(result))
                {
                    throw new ArgumentException(regex + ": Did not generate invalid string: " + result);
                }
            }

            return(result);
        }
예제 #2
0
        //Compile [:alpha:] [:punct:] etc
        public RegexNode CompileMacro(int index, int len)
        {
            AssertParse(len >= 0, "Cannot parse macro.");
            string substr   = mRegex.ToString().Substring(index, len);
            string expanded = null;

            switch (substr)
            {
            case "alnum": expanded = "[a-zA-Z0-9]"; break;

            case "alpha": expanded = "[a-zA-Z]"; break;

            case "upper": expanded = "[A-Z]"; break;

            case "lower": expanded = "[a-z]"; break;

            case "digit": expanded = "[0-9]"; break;

            case "xdigit": expanded = "[A-F0-9a-f]"; break;

            case "space": expanded = "[ \t]"; break;

            case "print": expanded = "[\\x20-\\x7F]"; break;

            case "punct": expanded = "[,;.!'\"]"; break;

            case "graph": expanded = "[\\x80-\\xFF]"; break;

            case "cntrl": expanded = "[\x00-\x1F\x7F]"; break;

            case "blank": expanded = "[ \t\r\n\f]"; break;

            case "guid": expanded = "[A-F0-9]{8}(-[A-F0-9]{4}){3}-[A-F0-9]{12}"; break;

            default: AssertParse(false, "Cannot parse macro."); break;
            }

            RegexCompiler subcompiler = new RegexCompiler();

            return(subcompiler.Compile(expanded));
        }