In this example you will check if string contains only numbers using Regex in C#
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Program { static void Main(string[] args) { string text = "144.52"; bool numeric = true; var regex = new Regex(@"^-?[0-9][0-9,\.]+$"); numeric = regex.IsMatch(text); if (numeric) Console.WriteLine("{0} is a number",text); else Console.WriteLine("{0} is not a number", text); Console.ReadLine(); } } |
Output: