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); }
public void LessThan_Negative_RegexDoesNotMatch_MorePositive() { string pattern = GetNumberRangePattern.ForLessThanOrEqualTo(-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); }
public void LessThanOrEqualTo_Negative_RegexMatches_Self() { string pattern = GetNumberRangePattern.ForLessThanOrEqualTo(-110); string matchTo = "-110"; var match = Regex.Match(matchTo, pattern); Assert.IsTrue(match.Success); Assert.AreEqual(matchTo, match.Value); }