using System; using System.IO; using System.Text.RegularExpressions; public class Test { public static void Main() { var ptrn = @"[^\x20-\x21\x23-\x25\x28-\x2E\x30-\x3B\x3F-\x7E\xA0-\xFF]"; var ptrn2 = @"^[\x20-\x21\x23-\x25\x28-\x2E\x30-\x3B\x3F-\x7E\xA0-\xFF]{0,40}$"; var str = " some text here "; if (str.Length > 40 || Regex.IsMatch(str, ptrn)) Console.WriteLine("1 - The line is too long or contains invalid char(s)!"); str = "1234567890 1234567890 1234567890 1234567890 1"; if (str.Length > 40 || Regex.IsMatch(str, ptrn)) Console.WriteLine("2 - The line is too long or contains invalid char(s)!"); str = "1234567890 1234567890 1234567890 1234567890"; if (str.Length > 40 || Regex.IsMatch(str, ptrn)) Console.WriteLine("3 - The line is too long or contains invalid char(s)!"); str = " some text here "; if (Regex.IsMatch(str, ptrn2)) Console.WriteLine("2.1 - The line is too long or contains invalid char(s)!"); str = "1234567890 1234567890 1234567890 1234567890 1"; if (Regex.IsMatch(str, ptrn2)) Console.WriteLine("2.2 - The line is too long or contains invalid char(s)!"); str = "1234567890 1234567890 1234567890 1234567890"; if ( Regex.IsMatch(str, ptrn2)) Console.WriteLine("2.3 - The line is too long or contains invalid char(s)!"); } }