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 stringExample 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
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-08-21 11:00:00 +0000
Seen: 19 times
Last updated: Jun 27 '22
How can user input be added to an array using a loop in C# and then displayed through printing?
If the chips occupy 80% of the space, what is the best way to package the remaining input?
What is the process for posting input values to a database?
What is the solution for aligning input vertically in different rows with Bootstrap 5?
How can user input be added to an empty nested array using useState?
How to provide Arduino input parameters of any size without creating a struct?