C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class Program { static void Main(string[] args) { //this section create string variables. string txt1 = "Lorem Ipsum 123"; string txt2 = "#Lorem Ipsum!123"; Console.WriteLine("=> " + txt1); Console.WriteLine("=> " + txt2); //check string contains any special characters without space Regex rex = new Regex("^[a-zA-Z0-9 ]+$"); Boolean result = rex.IsMatch(txt1); Boolean result2 = rex.IsMatch(txt2); Console.WriteLine("TEXT contains no special characters without space? " + result); Console.WriteLine("TEXT contains no special characters without space? " + result2); Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 |
=> Lorem Ipsum 123 => #Lorem Ipsum!123 TEXT contains no special characters without space? True TEXT contains no special characters without space? False |
Regular expression for numbers only in C#, C# regex replace only letters and numbers, Regex only letters and numbers, c# regex only letters, Regex keep only numbers and letters, C# Regex letters and numbers only,