Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can use the following C# regex pattern to ensure that a string has one dot followed by numbers:

^[0-9]+(\.[0-9]+)?$

Explanation:

  • ^ : Start of the string
  • [0-9]+ : Match one or more digits
  • (\.[0-9]+)? : Match a dot followed by one or more digits, optionally (the ? makes the group optional)
  • $ : End of the string

Example usage:

string input = "3.1415";
bool match = Regex.IsMatch(input, "^[0-9]+(\\.[0-9]+)?$"); // true

input = "42";
match = Regex.IsMatch(input, "^[0-9]+(\\.[0-9]+)?$"); // false

input = "3.14.15";
match = Regex.IsMatch(input, "^[0-9]+(\\.[0-9]+)?$"); // false