示例#1
0
        static void Main(string[] args)
        {
            do
            {
                Console.WriteLine("Hello. What number range would you like to match?");
                Console.WriteLine("You can write '<=AAA', '<AAA', '>=AAA', or '>AAA' or 'q' to quit.");
                Console.Write("Your range:  ");
                string range = Console.ReadLine();
                Console.WriteLine();
                var numbers = Regex.Matches(range, "[\\d\\-]+").OfType <Match>().Select(m => int.Parse(m.Value)).ToList();

                string pattern;
                if (range.StartsWith("<=") && numbers.Count == 1)
                {
                    pattern = GetNumberRangePattern.ForLessThanOrEqualTo(numbers[0]);
                }
                else if (range.StartsWith("<") && numbers.Count == 1)
                {
                    pattern = GetNumberRangePattern.ForLessThan(numbers[0]);
                }
                else if (range.StartsWith(">=") && numbers.Count == 1)
                {
                    pattern = GetNumberRangePattern.ForGreaterThanOrEqualTo(numbers[0]);
                }
                else if (range.StartsWith(">") && numbers.Count == 1)
                {
                    pattern = GetNumberRangePattern.ForGreaterThan(numbers[0]);
                }
                else if (range.ToLower().StartsWith("q"))
                {
                    return;
                }
                else
                {
                    Console.WriteLine("Invalid format. Please try again.");
                    continue;
                }

                Console.WriteLine($"The regex pattern to match '{range}' is:");
                Console.WriteLine(pattern);
                Console.WriteLine();

                Console.Write("What number would you like to check with this pattern? ");
                var toCheck = Console.ReadLine();
                var check   = Regex.IsMatch(toCheck, pattern);

                if (check)
                {
                    Console.WriteLine($"Success! {toCheck} meets the condition {range}.");
                }
                else
                {
                    Console.WriteLine($"No, {toCheck} does not meet the condition {range}.");
                }
                Console.ReadLine();
                Console.Clear();
            } while (true);
        }
示例#2
0
        public void LessThan_Negative_RegexDoesNotMatch_MorePositive()
        {
            string pattern = GetNumberRangePattern.ForLessThan(-9);

            Assert.IsFalse(Regex.Match("-8", pattern).Success);
            Assert.IsFalse(Regex.Match("0", pattern).Success);
            Assert.IsFalse(Regex.Match("9", pattern).Success);
            Assert.IsFalse(Regex.Match("999", pattern).Success);
        }
示例#3
0
        public void LessThan_Negative_RegexMatches_MuchSmaller()
        {
            string pattern = GetNumberRangePattern.ForLessThan(-51);
            string matchTo = "-525252525252";

            var match = Regex.Match(matchTo, pattern);

            Assert.IsTrue(match.Success);
            Assert.AreEqual(matchTo, match.Value);
        }
示例#4
0
        public void LessThan_0_RegexMatches_VeryNegative()
        {
            string pattern = GetNumberRangePattern.ForLessThan(0);
            string matchTo = "-999999999999999999999999999999999";

            var match = Regex.Match(matchTo, pattern);

            Assert.IsTrue(match.Success);
            Assert.AreEqual(matchTo, match.Value);
        }
示例#5
0
        public void LessThan_Positive_RegexMatches_0()
        {
            string pattern = GetNumberRangePattern.ForLessThan(9);
            string matchTo = "0";

            var match = Regex.Match(matchTo, pattern);

            Assert.IsTrue(match.Success);
            Assert.AreEqual(matchTo, match.Value);
        }