fork download
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. public class Test {
  5. public static void Main() {
  6. Console.WriteLine("Objective: a regex that matches only printable characters");
  7. Console.WriteLine("Should not match: \x04");
  8. Console.WriteLine("Should match: *, A");
  9.  
  10. var regexesToTest = new string[] {
  11. @"![\P{Cc}\P{Cn}\P{Cs}]",
  12. @"![^\p{Cc}^\p{Cn}^\p{Cs}]"
  13. };
  14. foreach (var regexExpression in regexesToTest) {
  15. Console.WriteLine();
  16. Console.WriteLine("Testing the regex: " + regexExpression);
  17. var regex = new Regex(regexExpression);
  18. foreach (var testCharacter in new char[] { (char)4, '*', 'A' }) {
  19. Console.WriteLine(" Testing whether it matches character: " + testCharacter);
  20. var matches = regex.Matches("Hello, World!" + testCharacter);
  21. Console.WriteLine(" Results: " + matches.Count);
  22. foreach (Match match in matches) {
  23. Console.WriteLine(" Result: " + match);
  24. }
  25. }
  26. }
  27. }
  28. }
  29.  
Success #stdin #stdout 0.06s 29464KB
stdin
Standard input is empty
stdout
Objective: a regex that matches only printable characters
Should not match: 
Should match: *, A

Testing the regex: ![\P{Cc}\P{Cn}\P{Cs}]
  Testing whether it matches character: 
    Results: 1
    Result: !
  Testing whether it matches character: *
    Results: 1
    Result: !*
  Testing whether it matches character: A
    Results: 1
    Result: !A

Testing the regex: ![^\p{Cc}^\p{Cn}^\p{Cs}]
  Testing whether it matches character: 
    Results: 0
  Testing whether it matches character: *
    Results: 1
    Result: !*
  Testing whether it matches character: A
    Results: 1
    Result: !A